Qt: Undefined reference to 'vtable' [duplicate]

北慕城南 提交于 2020-02-02 16:32:26

问题


I am working on a program in Qt using c++. But i got stuck at this point because of this error. The weird thing is that i created a separate program in which the following code worked but when i put the code inside my program i get an error.

error: undefined reference to `vtable for Create_button_config'

The error is in the Header file in which i create the class. This is the part of the header file where the error occurs.

class Create_button_config : public QObject
{
    Q_OBJECT

public:

    QMap<QString, QString> buttons;

    void setParameters(){
        qDebug() << "test";
        buttons["ID1"] = "#52B1";
        buttons["ID2"] = "#52B2";
        buttons["ID3"] = "#52B3";

    }
};

And i use it in main.cpp like this

Create_button_config config;
config.setParameters();

Any ideas where this error comes from? And by the way is this the correct way to make an associative array which is available throughout my whole code?

Thanks in advance


回答1:


Absence of a vtable is usually a symptom of failing to include the output of moc in the linker arguments. Make sure that you ran moc on your header, and that you linked the result.

Note that if you're using qmake, you may need to re-run qmake to generate new makefiles if you change a class that was not Q_OBJECT so that is now Q_OBJECT - it will not otherwise know that moc should be run.

In passing, it's a good idea to add a constructor that takes an optional parent QObject, to get some of the benefits of Qt's memory management (freeing of child objects) where the user wants it.



来源:https://stackoverflow.com/questions/31205877/qt-undefined-reference-to-vtable

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