1

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

Martijn
  • 112
  • 1
  • 10
  • 6
    Try "Run qmake" – Jablonski Jul 03 '15 at 11:35
  • Take a look at similar question here -- http://stackoverflow.com/questions/2555816/qt-object-linker-problem-undefined-reverence-to-vtable Also, do not forget to `make clean` and even clean by hands of your project before doing `qmake` and rebuilding. – NG_ Jul 03 '15 at 11:53
  • 1
    I am not sure if something goes wrong with my system, but for me **deleting the build directory** fixed the problem. (Even "Clean all" was not enough). I guess it is because the `moc` (generated) file could not access the header. I hope this will help someone. – willy Mar 25 '17 at 11:25

1 Answers1

11

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Thanks! I thought i had already rebuild the complete project and at that time it did not work. But i came back to it a couple of days later and now it is working. Thanks for your help! – Martijn Jul 09 '15 at 08:57