Possible Duplicate:
Qt: Signals and slots Error: undefined reference to `vtable for
Here we have test.cpp:
#include <QApplication>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Place alone in a new directory and:
$ qmake -project
$ qmake
$ make
It doesn't work:
test.o: In function `MainWindow::~MainWindow()':
test.cpp:(.text._ZN10MainWindowD2Ev[_ZN10MainWindowD5Ev]+0x3): undefined reference to `vtable for MainWindow'
test.cpp:(.text._ZN10MainWindowD2Ev[_ZN10MainWindowD5Ev]+0xb): undefined reference to `vtable for MainWindow'
test.o: In function `main':
test.cpp:(.text.startup+0x48): undefined reference to `vtable for MainWindow'
test.cpp:(.text.startup+0x51): undefined reference to `vtable for MainWindow'
test.o: In function `MainWindow::~MainWindow()':
test.cpp:(.text._ZN10MainWindowD0Ev[_ZN10MainWindowD0Ev]+0x7): undefined reference to `vtable for MainWindow'
test.o:test.cpp:(.text._ZN10MainWindowD0Ev[_ZN10MainWindowD0Ev]+0xf): more undefined references to `vtable for MainWindow' follow
collect2: error: ld returned 1 exit status
make: *** [tmp] Error 1
In general such errors are either because moc is not invoked or because of unimplemented virtual methods.
moc should be invoked automatically by qmake, and afaik QMainWindow doesn't have any pure virtual methods - so what is the problem here?
When I remove Q_OBJECT it works. Why is this? What is going on?
I see that Q_OBJECT tells moc to generate the signals/slots meta-data for that object, why can't it do that in this case?
Update:
Solution is to add #include "test.moc"
just below class:
#include <QApplication>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
};
#include "test.moc" // <----------- HERE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}