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?



  • This post is deleted!


  • @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 a QFileInfo *fileInfo, or more usually/easier/like the way Qt functions tend to pass QObjects around, use a reference: QFileInfo &fileInfo.


  • Lifetime Qt Champion

    @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.



  • @SGaist
    Sorry, I mis-read QFileInfo for 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.



  • @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; with stream<< endl;. See http://doc.qt.io/qt-5/qtextstream.html#endl

    if (!file->open(QIODevice::WriteOnly))

    If you are intending to write text, open the file in text modeif (!file->open(QIODevice::WriteOnly | QIODevice::Text))


Log in to reply