Nothing helped to solve “Undefined reference to vtable” in Qt

送分小仙女□ 提交于 2021-01-28 02:57:25

问题


I can't build this with error "undefined reference to vtable for CustomUndoStack"

Here's the code:

class CustomUndoStack : public QObject
{
    Q_OBJECT

public:

};


int main(int argc, char *argv[])
{
    QCoreApplication qCoreApplication(argc, argv);

    CustomUndoStack uStack;

    return qCoreApplication.exec();
}

Here's .pro file:

QT += widgets

CONFIG += c++14 console
CONFIG -= app_bundle

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += main.cpp

I've deleted manually the build directory, I've tried "Clean All" and "Rebuild All" in all possible ways, I've run qmake 100500 times, I've changed CustomUndoStack interface over 9000 times, but nothing helps.

Where's the problem?

UPD to answer the comment: if I remove Q_OBJECT line then I can't do this. It says that "No such signal QObject::undoSignal()"

class CustomUndoStack : public QObject
{
//    Q_OBJECT

public:
    CustomUndoStack() {
        connect(this, SIGNAL(undo_signal()), &undoStack, SLOT(undo()));
    }
signals:
    void undo_signal();
private:
    QUndoStack undoStack;
};

回答1:


Your problem is caused by the fact that you have the declaration of a class subclassing QObject and having Q_OBJECT macro inside it within a .cpp file; That macro contains, among other things, the declaration of a virtual method:

virtual const QMetaObject *metaObject() const;

qmake tool only processes header files to locate QObject subclasses containing Q_OBJECT macro to automatically generate the actual code corresponding to the declarations produced by Q_OBJECT macro. In your case, qmake does not process main.cpp file and hence it is left containing unimplemented virtual function - hence the linker error you are receiving.

The proper solution is to move the declaration of the class to a separate header file and add the header to your project. There's, however, another solution which is kinda hacky but it can be used if you for some reason absolutely need to leave the declaration of your class inside the .cpp file:

class CustomUndoStack : public QObject
{
    Q_OBJECT
public:
    CustomUndoStack(QObject * parent = 0) : QObject(parent) {}
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QCoreApplication qCoreApplication(argc, argv);

    CustomUndoStack uStack;

    return qCoreApplication.exec();
}

Notice the #include "main.moc" line. The presence of this line forces qmake to actually process the declarations from your .cpp file and generate the required .moc file which contains the autogenerated code corresponding to the declarations produced by Q_OBJECT macro.



来源:https://stackoverflow.com/questions/46926982/nothing-helped-to-solve-undefined-reference-to-vtable-in-qt

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