Call to Implicitly deleted copy constuctor; How can I structure this class properly?
-
@Smeeth said in Call to Implicitly deleted copy constuctor; How can I structure this class properly?:
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. -
@Smeeth
As @mpergand says, you cannot copy.So in the case of a function parameter,
FileWriter::FileWriter(QFileInfo fileInfo)
, you could either define yours as taking aQFileInfo *fileInfo
, or more usually/easier/like the way Qt functions tend to passQObject
s around, use a reference:QFileInfo &fileInfo
. -
@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 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. -
@Smeeth said in Call to Implicitly deleted copy constuctor; How can I structure this class properly?:
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<< FileWriter::NEW_LINE;
withstream<< endl;
. See http://doc.qt.io/qt-5/qtextstream.html#endlif (!file->open(QIODevice::WriteOnly))
If you are intending to write text, open the file in text mode
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))