Tuesday, February 9th 2010, 10:19am UTC

You are not logged in.

  • Login
  • Register

1

Friday, January 23rd 2004, 10:37pm

Fixing "undefined reference to `vtable ..."

Hi All,

Sometimes the linking stage of compiling might fail with the rather confusing error:

undefined reference to `vtable for MyWidget`

The short answer is:
Don't put class definitions in .cpp files when they have the Q_OBJECT macro.


ie if there are signals or slots in a class, ensure the definition is in a header file. Otherwise, moc doesn't see it and do all its magic, and so a lot of required code isn't generated.



Q: What is a vtable? A: Virtual Table

The vtable is an internal structure used for virtual overriding etc, which is heavily used in QT.
ie:
class MyWidget : public QWidget
{
Q_OBJECT

public slots:
virtual void show(); // vtable will be used for this.
};


Hope that helps someone :)

Debboy
  • Go to the top of the page

rms

Beginner

2

Sunday, January 25th 2004, 1:25pm

RE: Fixing "undefined reference to `vtable ..."

There is a way to put subclasses of QObject in a .cpp file. If your .cpp file is foo.cpp, then just put a

#include "foo.moc"

somewhere in the foo.cpp file (and run qmake again).

Rainer
  • Go to the top of the page

Rate this thread