Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Call to Implicitly deleted copy constuctor; How can I structure this class properly?
Smeeth last edited by Smeeth
This post is deleted!
mpergand last edited by mpergand
public: FileInfo(QFileInfo fileInfo);
I guess you mean FileWriter ...
I suspect this is because I am storing a QFile* which doesn't have a copy constructor?
You're right, QFile derives from QObject which is uncopyable.
It's clearly explain in the QObject doc:
No Copy Constructor or Assignment Operator
QObject has neither a copy constructor nor an assignment operator. This is by design.
The main consequence is that you should use pointers to QObject (or to your QObject subclass)
where you might otherwise be tempted to use your QObject subclass as a value.
For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes.
You must store pointers.
JonB last edited by
So in the case of a function parameter,
FileWriter::FileWriter(QFileInfo fileInfo), you could either define yours as taking a
QFileInfo *fileInfo, or more usually/easier/like the way Qt functions tend to pass
QObjects around, use a reference:
@JonB QFileInfo is not a QObject based class you can see that on the top of the documentation. There's no inherits field in the table and furthermore there's a copy constructor and an assignment operator. That said, using
const QFileInfo &as parameter type for the constructor will avoid a useless copy.
JonB last edited by
Sorry, I mis-read
QFile. Re-reading carefully, where in OP's code is the
when I try to return that object from a method
Anyway, if you feel my posts are confusing this issue please feel free to delete them.
VRonin last edited by
This is giving me a "Call to Implicitly deleted copy constructor" error when I try to return that object from a method.
What object? what method?
const static QString NEW_LINE;
This is useless, replace
stream<< endl;. See http://doc.qt.io/qt-5/qtextstream.html#endl
If you are intending to write text, open the file in text mode
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))