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. 'QString' and 'QString&' issue

'QString' and 'QString&' issue

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k 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.
  • goloviznin.kG Offline
    goloviznin.kG Offline
    goloviznin.k
    wrote on last edited by
    #1

    This morning I was unable to build the project due to a large number of 'C2664' errors - it is not possible to convert 'Smoething' to 'Something&'. For example, 'QString' to 'QString &' in my functions like void do_stuff (QString & str, ...); which I used like this: obj.do_stuff (QString ("Some nice string"), ...);

    Exactly the same code worked last night.
    I tried going back to the old version of the project, update Qt, update VS, Qt VS Tools - most recent - nothing helped.
    Then I just changed the function arguments to 'QString' instead of 'QString&' etc in the entire project. Everything started to work.

    Everything is fine now, but maybe someone has an idea what actually happened and why everything worked until today?

    I have used VS2019, Win10, Qt 5.12.10

    Kirill

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @goloviznin-k said in 'QString' and 'QString&' issue:

      Exactly the same code worked last night.

      Maybe - but MSVC was wrong there since ages and did not complain about it. Maybe you updated MSVC.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      goloviznin.kG 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @goloviznin-k said in 'QString' and 'QString&' issue:

        Exactly the same code worked last night.

        Maybe - but MSVC was wrong there since ages and did not complain about it. Maybe you updated MSVC.

        goloviznin.kG Offline
        goloviznin.kG Offline
        goloviznin.k
        wrote on last edited by
        #3

        @Christian-Ehrlicher If I understand everything correctly, I could only update it using the Qt maintenance tool. It is not updating by itself, is it? I only updated it after a problem appeared while trying to fix the project.
        But Qt VS Tools updates by itself, maybe it broke everything.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I meant the MSVC compiler (because it's the one who's now correctly complaining about it). The interesting thing is that C2664 exists for a long time but I'm pretty sure I had problems with such stuff not a long time ago.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          goloviznin.kG 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            I meant the MSVC compiler (because it's the one who's now correctly complaining about it). The interesting thing is that C2664 exists for a long time but I'm pretty sure I had problems with such stuff not a long time ago.

            goloviznin.kG Offline
            goloviznin.kG Offline
            goloviznin.k
            wrote on last edited by
            #5

            @Christian-Ehrlicher I understand that you were talking about the compiler. I'm surprised it updated itself somehow, because I don't update anything as long as everything works.

            Anyway thanks for the answer, maybe it will help someone in the future

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @goloviznin-k said in 'QString' and 'QString&' issue:

              do_stuff (QString & str, ...); which I used like this: obj.do_stuff (QString ("Some nice string"), ...);

              You are receiving that error because you cannot pass a temporary object to a function expecting a reference to a mutable object. To quote the Microsoft docs, "If a temporary object is passed to a function that takes a reference to an object as a parameter, that reference must be a const reference."

              This is not a new behaviour in the Microsoft compiler (since at least VS2015), or even unique to Microsoft. You get a similar error from GCC on Linux.

              main.cpp: In function ‘int main(int, char**)’:
              main.cpp:7:7: error: cannot bind non-const lvalue reference of type ‘QString&’ to an rvalue of type ‘QString’
                  7 |  test(QString("Xyzzy"));
                    |       ^~~~~~~~~~~~~~~~~~
              main.cpp:4:20: note:   initializing argument 1 of ‘void test(QString&)’
                  4 | void test(QString &s) { qDebug() << s; }
                    |           ~~~~~~~~~^
              make: *** [Makefile:358: main.o] Error 1
              

              Using a non-const reference implies that the function needs to be able to/will modify the object passed to it. If passing the string by value gives you the expected behaviour outside the function, i.e. the object passed in is not expected to be changed, then a pass by const reference should work also.

              goloviznin.kG 1 Reply Last reply
              1
              • C ChrisW67

                @goloviznin-k said in 'QString' and 'QString&' issue:

                do_stuff (QString & str, ...); which I used like this: obj.do_stuff (QString ("Some nice string"), ...);

                You are receiving that error because you cannot pass a temporary object to a function expecting a reference to a mutable object. To quote the Microsoft docs, "If a temporary object is passed to a function that takes a reference to an object as a parameter, that reference must be a const reference."

                This is not a new behaviour in the Microsoft compiler (since at least VS2015), or even unique to Microsoft. You get a similar error from GCC on Linux.

                main.cpp: In function ‘int main(int, char**)’:
                main.cpp:7:7: error: cannot bind non-const lvalue reference of type ‘QString&’ to an rvalue of type ‘QString’
                    7 |  test(QString("Xyzzy"));
                      |       ^~~~~~~~~~~~~~~~~~
                main.cpp:4:20: note:   initializing argument 1 of ‘void test(QString&)’
                    4 | void test(QString &s) { qDebug() << s; }
                      |           ~~~~~~~~~^
                make: *** [Makefile:358: main.o] Error 1
                

                Using a non-const reference implies that the function needs to be able to/will modify the object passed to it. If passing the string by value gives you the expected behaviour outside the function, i.e. the object passed in is not expected to be changed, then a pass by const reference should work also.

                goloviznin.kG Offline
                goloviznin.kG Offline
                goloviznin.k
                wrote on last edited by
                #7

                @ChrisW67 Thank you for your answer. I should have probably changed the argument from 'QString&' to 'const QString&' rather than just 'QString'.

                Perhaps Qt has nothing to do with it. It's just that the MSVC compiler got tired of having it this way and decided that in 2022 we will do the right thing. But I haven't updated it. This is the machine rebellion. Against bad code I guess.

                1 Reply Last reply
                0

                • Login

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