Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. binary '%': 'QString' does not define this operator
Forum Updated to NodeBB v4.3 + New Features

binary '%': 'QString' does not define this operator

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 820 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Felix_F_.-
    wrote on 11 Jun 2023, 01:59 last edited by
    #1

    Hi,

    I'm trying to compile the QT source code for fun and I had an error at this code:

    void toString(QString &appendTo, IPv4Address address)
    {
        // reconstructing is easy
        // use the fast operator% that pre-calculates the size
        appendTo += number(address >> 24) % u'.'
                    % number(address >> 16) % u'.'
                    % number(address >> 8) % u'.'
                    % number(address);
    }
    

    the error is:

    error C2676: binary '%': 'QString' does not define this operator or a conversion to a type acceptable to the predefined operator

    I must have some mistakes as to my local dev envirionment, which file / class should I debug into?

    J 1 Reply Last reply 11 Jun 2023, 06:51
    0
    • F Offline
      F Offline
      Felix_F_.-
      wrote on 11 Jun 2023, 10:49 last edited by
      #4

      I figured out that I need to define QT_USE_QSTRINGBUILDER during building of qt source code (https://stackoverflow.com/a/12187069/19171308)

      and then operator% is defined:

      template <typename A, typename B>
      QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
      operator%(const A &a, const B &b)
      {
         return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
      }
      
      J 1 Reply Last reply 11 Jun 2023, 11:27
      2
      • F Felix_F_.-
        11 Jun 2023, 01:59

        Hi,

        I'm trying to compile the QT source code for fun and I had an error at this code:

        void toString(QString &appendTo, IPv4Address address)
        {
            // reconstructing is easy
            // use the fast operator% that pre-calculates the size
            appendTo += number(address >> 24) % u'.'
                        % number(address >> 16) % u'.'
                        % number(address >> 8) % u'.'
                        % number(address);
        }
        

        the error is:

        error C2676: binary '%': 'QString' does not define this operator or a conversion to a type acceptable to the predefined operator

        I must have some mistakes as to my local dev envirionment, which file / class should I debug into?

        J Offline
        J Offline
        JonB
        wrote on 11 Jun 2023, 06:51 last edited by
        #2

        @Felix_F_
        What version of Qt?
        What compiler?
        What compiler options and/or Qt configure options?
        What file did you paste this from?

        I am not as expert at C++ as some here, but https://doc.qt.io/qt-6/qstring.html shows no indication that QString overrides the % operator. Here it would need to be the overridden + operator to achieve what is desired.

        F 1 Reply Last reply 11 Jun 2023, 10:30
        0
        • J JonB
          11 Jun 2023, 06:51

          @Felix_F_
          What version of Qt?
          What compiler?
          What compiler options and/or Qt configure options?
          What file did you paste this from?

          I am not as expert at C++ as some here, but https://doc.qt.io/qt-6/qstring.html shows no indication that QString overrides the % operator. Here it would need to be the overridden + operator to achieve what is desired.

          F Offline
          F Offline
          Felix_F_.-
          wrote on 11 Jun 2023, 10:30 last edited by
          #3

          @JonB branch v6.5.1
          the code is actually QT's source code, see https://github.com/qt/qtbase/blob/5ea0256b07495977a1f2740f6b2d99984cf927dc/src/corelib/io/qipaddress.cpp#L97

          I also tried to find an operator% definition but I didn't find it.

          from the comment, you can see it's '%' not '+'.

          // use the fast operator% that pre-calculates the size

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Felix_F_.-
            wrote on 11 Jun 2023, 10:49 last edited by
            #4

            I figured out that I need to define QT_USE_QSTRINGBUILDER during building of qt source code (https://stackoverflow.com/a/12187069/19171308)

            and then operator% is defined:

            template <typename A, typename B>
            QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
            operator%(const A &a, const B &b)
            {
               return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
            }
            
            J 1 Reply Last reply 11 Jun 2023, 11:27
            2
            • F Felix_F_.- has marked this topic as solved on 11 Jun 2023, 10:49
            • F Felix_F_.-
              11 Jun 2023, 10:49

              I figured out that I need to define QT_USE_QSTRINGBUILDER during building of qt source code (https://stackoverflow.com/a/12187069/19171308)

              and then operator% is defined:

              template <typename A, typename B>
              QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
              operator%(const A &a, const B &b)
              {
                 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
              }
              
              J Offline
              J Offline
              JonB
              wrote on 11 Jun 2023, 11:27 last edited by
              #5

              @Felix_F_
              Interesting.

              There's also mention of the QT_USE_QSTRINGBUILDER macro, which turns all + usage into %, provided that doesn't create problems elsewhere on your code.

              But FWIW still no mention of this possibly-defined % operator in QString docs page.

              C 1 Reply Last reply 12 Jun 2023, 01:11
              0
              • J JonB
                11 Jun 2023, 11:27

                @Felix_F_
                Interesting.

                There's also mention of the QT_USE_QSTRINGBUILDER macro, which turns all + usage into %, provided that doesn't create problems elsewhere on your code.

                But FWIW still no mention of this possibly-defined % operator in QString docs page.

                C Offline
                C Offline
                ChrisW67
                wrote on 12 Jun 2023, 01:11 last edited by
                #6

                @JonB It is there "sort of": More Efficient String Construction (also in Qt 6).

                The operator%() function is not part of the QString class, which may explain why it is not documented directly there.

                J 1 Reply Last reply 12 Jun 2023, 07:47
                1
                • C ChrisW67
                  12 Jun 2023, 01:11

                  @JonB It is there "sort of": More Efficient String Construction (also in Qt 6).

                  The operator%() function is not part of the QString class, which may explain why it is not documented directly there.

                  J Offline
                  J Offline
                  JonB
                  wrote on 12 Jun 2023, 07:47 last edited by
                  #7

                  @ChrisW67
                  Thanks. I don't totally understand how this works. Given that you write QString % QString, why does C++ go off and match that against some QStringBuilder template?

                  1 Reply Last reply
                  0

                  1/7

                  11 Jun 2023, 01:59

                  • Login

                  • Login or register to search.
                  1 out of 7
                  • First post
                    1/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved