I've just started programming in Qt framework. Following is a very simple program:
#include <QtCore/QCoreApplication> #include <QDebug> class MyClass : public QObject { Q_OBJECT public: MyClass() {} }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyClass *c = new MyClass(); return a.exec(); } But I receive following error when I try to compile & run it:
In function MyClass:
undefined reference to vtable for MyClass
But when I remove the QObject macro everything works fine. Please note that the class is defined in the same file as the main function.
I'm using Qt version 4.7, running on Win 7.
What is causing this issue?
Update: I get the same error when I define my class in a separate header file. mytimer.h:
#ifndef MYTIMER_H #define MYTIMER_H #include <QtCore> class MyTimer : public QObject { Q_OBJECT public: QTimer *timer; MyTimer(); public slots: void DisplayMessage(); }; #endif // MYTIMER_H mytimer.cpp:
#include "mytimer.h" #include <QtCore> MyTimer::MyTimer() { timer = new QTimer(); connect(timer,SIGNAL(timeout()),this,SLOT(DisplayMessage())); timer->start(1000); } void MyTimer::DisplayMessage() { qDebug() << "timed out"; } And this is the main.cpp:
#include <QtCore/QCoreApplication> #include <QDebug> #include "mytimer.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyTimer *mt = new MyTimer(); return a.exec(); }