问题
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