when is a QString not really as QString - argument rejected by QFile
-
hi folks
this code (Qt V6, Windows 11)
bool Game::saveGame(const QString filename, const FileFormat format) const { QFile fn(filename);is driving me nuts. As you can see i want to save data into a json file using the name in argument "filename"
Visual Studio Code is reporting that QFile does not have a constructor that matches this argument list and that the argument is of type QString .. so .. the error might even be in VSC ??
i have also tried filename.ToStdString() to see if it would use the QFile constructor that will take the file name that way .. same problem
I tried making a file info QFileInfor infoname(filename) .. that works .. but then
QFile nf( infoname.FilePath() ) still has no constructor for a QStringhelp?
-
yes .. except the error flag is on the name of the QFile, "fn" .. but .. GitHUb CoPilot found a solution .. if i can screen shot the answer
i changed the code to
// Save Game object to file bool Game::saveGame( const QString filename, const FileFormat format) const { QFile fn(); fn.setFileName( filename ); if( fn.exists() ) { std::cerr << std::string("Error: File already exists: ") << fn.fileName().toStdString() << std::endl; return false; }and that changed the message to this
expression must have class type but it has type "QFile (*)()"
and now all the reference to "fn" are flagged as errors
it took a beat but the AI Proposed this as the root of the problem
The problem is that QFile fn() is being interpreted as a function declaration rather than object construction. Remove the parentheses to properly declare a QFile object.
and it changed the code to this

so you see any difference? cause i do not .. but it is fixed so ..
-
yes .. except the error flag is on the name of the QFile, "fn" .. but .. GitHUb CoPilot found a solution .. if i can screen shot the answer
i changed the code to
// Save Game object to file bool Game::saveGame( const QString filename, const FileFormat format) const { QFile fn(); fn.setFileName( filename ); if( fn.exists() ) { std::cerr << std::string("Error: File already exists: ") << fn.fileName().toStdString() << std::endl; return false; }and that changed the message to this
expression must have class type but it has type "QFile (*)()"
and now all the reference to "fn" are flagged as errors
it took a beat but the AI Proposed this as the root of the problem
The problem is that QFile fn() is being interpreted as a function declaration rather than object construction. Remove the parentheses to properly declare a QFile object.
and it changed the code to this

so you see any difference? cause i do not .. but it is fixed so ..
@bitbasher said in when is a QString not really as QString - argument rejected by QFile:
so you see any difference? cause i do not .. but it is fixed so ..
QFile fn();
--> function call
QFile fn;
--> ctor
-
hi folks
this code (Qt V6, Windows 11)
bool Game::saveGame(const QString filename, const FileFormat format) const { QFile fn(filename);is driving me nuts. As you can see i want to save data into a json file using the name in argument "filename"
Visual Studio Code is reporting that QFile does not have a constructor that matches this argument list and that the argument is of type QString .. so .. the error might even be in VSC ??
i have also tried filename.ToStdString() to see if it would use the QFile constructor that will take the file name that way .. same problem
I tried making a file info QFileInfor infoname(filename) .. that works .. but then
QFile nf( infoname.FilePath() ) still has no constructor for a QStringhelp?
@bitbasher said in when is a QString not really as QString - argument rejected by QFile:
the error might even be in VSC ??
Does the compiler complain or Visual studio code?
bool saveGame(const QString filename) { QFile fn(filename); return fn.exists(); }This compiles fine.
-
For completeness, here's a copy of some examples from https://en.cppreference.com/w/cpp/language/initialization.html:
#include <string> std::string s1; // default-initialization std::string s2(); // NOT an initialization! // actually declares a function “s2” // with no parameter and returns std::string std::string s3 = "hello"; // copy-initialization std::string s4("hello"); // direct-initialization std::string s5{'a'}; // list-initialization (since C++11) char a[3] = {'a', 'b'}; // aggregate initialization // (part of list initialization since C++11) char& c = a[0]; // reference initializationSo indeed there's a big difference between fn; and fn(); :-)
-
In the C++ world don't pass mutable objects by const value. Pass them by const reference. ie
const QString& filenameQFile follows this paradigm...