Qt compile error: C2280: attempting to reference a deleted function

只谈情不闲聊 提交于 2021-02-08 08:41:50

问题


I have a relatively simple class that contains a layout with a few widgets (labels, lineEdits, pushButtons) that gets displayed as a card. It all works fine until I try to add signals and slots. The header file, below, compiles fine with the sections commented-out as shown.

#include <QString>
#include <QObject>
#include <QWidget>

class KMLFile //: public QObject
{
    //Q_OBJECT
public:
    KMLFile();
    ~KMLFile();

    QString m_originalFilename;
    QString m_originalPath;
    QString m_proposedFilename;
    QString m_propsoedPath;
    QString m_coords;
    QWidget* trackWidget;

    void populate(QString originalFilename, QString originalPath, QString proposedFilename, QString coords);
    QString getCoords();
    int getLength();

//public slots:
    //void changeFilename();

};

When those bits of code are left in, I get the following error:

"C:...\qlist.h:425: error: C2280: 'KMLFile::KMLFile(const KMLFile &)': attempting to reference a deleted function

I am using Qt5.3 and Qt Creator. I have tried cleaning, running qmake and deleting the build folder to no avail. I'm stumped!

Grateful for any insight into how to fix this so that I can progress.


回答1:


The copy constructor of QObject is private - or deleted.

Somewhere else in your code you use the copy constructor of KMLFile, which then calls the copy constructor of QObject.

You could reimplement the copy constructor of KMLFile, but the best solution is to not use it at all.

In your QList you should store pointers to the KMLFile objects instead of the objects themselves provided that they will live longer than the list itself. Or you can start using QSharedPointer.




回答2:


QObject has a deleted copy constructor. Your class is creating a compiler generated copy constructor. When you make QObject a base of your class the compiler generated copy constructor for KLMFile attempts to call the deleted copy constructor of the base class and that is when you get an error.

The copy constructor for KLMFile will be automatically generated if required (ie if you try copy an instance of the class) unless you explicitly specify a copy constructor of your own.

If the line referenced in the error is near a copy of the KLMFile class that would be a strong indicator that this is the case.



来源:https://stackoverflow.com/questions/37396950/qt-compile-error-c2280-attempting-to-reference-a-deleted-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!