Question

I'm developing a simple Qt 4 app and making my own dialog. I subclassed QDialog, inserted the Q_OBJECT macro in the class declaration block, and... I get

[Linker error] undefined reference to `vtable for MyDialog' and there is no moc_MyDialog.cpp generated by the moc compiler.

I am using Qt 4.1.3 on Windows XP and mingw. I followed the build process from the Qt-supplied build shell. I used qmake to create make files and compiled everything with a make command.

I have other classes that subclass QPushButton and QObject respectively, but they compile OK. I can't find any differences between them and the broken one.

There must be missing something in the broken class, but I'm unable to spot it.

Was it helpful?

Solution

The undefined reference to "vtable for MyDialog" is caused because there is no moc file. Most c++ compilers create the vtable definition in the object file containing the first virtual function. When subclassing a qt object and using the Q_OBJECT macro, this will be in the moc*.cpp file. Therefore, this error means that the moc file is missing.

The possible problems I can think of are:

  1. The header file for the class MyDialog.h is not added to HEADERS in the qmake file.

  2. You ran qmake to generate the make file before adding the Q_OBJECT macro. This created a make file without the moc rules. This is easily fixed by simply running qmake again.

  3. Your dialog derives from more than one class and QDialog is not the first class that it derives from. For qmake to work correctly, the QObject derived base class needs to be the first class that is inherited from.

  4. If you are using Qt Creator, you might get this error if your previous deployment was failed due to some reason (like application already running). In that case, simply do a 'Clean Project' and then 'Rebuild Project' and then 'Run' to deploy.

OTHER TIPS

If you have your header file included, follow the steps:

  1. Right click on the project where you have added this.
  2. Hit 'Run qmake'.

This will will clear up the old references and build with th Q_OBJECT macro. QT doesn't do it on rebuild.

I have see that the problem appear only when is added a class with no extension and then an extension is added manually.

To solve the problem i put Q_OBJECT in the .h of the class and then right click on "Sources" -> "Add Existing Files..." selecting the .ccp of my modified class.

Are you using qmake? Perhaps you didn't add it to your the .cpp file to your SOURCES and .h file to your HEADERS variable in the qmake file?

I humbly suggest you use CMake for building Qt programs on Windows. It will keep you remember to add appropriate files to its build files.

The additional value is that you can generate make/nmake build files from it, Visual Studio solution files. And if you compile Qt from source for Visual Studio you will be able to both code and build with MS IDE/compiler.

This is of course if you are using Visual Studio at all.

The message undefined reference to `vtable for MyDialog' can also be the result of a missing implementation (in MyDialog) of a pure virtual function in a class that MyDialog is derived from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top