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