Possible Duplicate:
Undefined reference to vtable. Trying to compile a Qt project
here is the code
#include <iostream>
#include <QApplication>
#include <QTimer>
class myClass : public QObject {
Q_OBJECT
public:
QTimer *timer;
myClass(){
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(mySlot()));
timer->start(1000);
}
public slots:
void mySlot() {
std::cout << "Fire" << std::endl;
}
};
int main() {
std::cout << "Hello, world";
myClass atimer;
return 0;
}
Apart from the error, there are two more things I don’t understand:
-
Why there isn’t any semicolon after macros, in this case Q_OBJECT. It doesn’t seem to obey C++ syntax but yet people write code like that.
-
The “public slots” is a modifier created by Qt, but how come gcc compiler can still understand it. How could an IDE like Qt modify the standard syntax of a language?
You didn’t give the exact error message, but I suspect what’s happening is that you didn’t run
mocon your code, or you didn’t compile the code generated by moc, or you didn’t link the code into your executable/library.As for your other questions:
You don’t need to have a semicolon after macros; the preprocessor doesn’t care about semicolons – only the compiler does. So whether or not you need to add a semicolon manually depends on what your macro (
Q_OBJECT) in this case expands to, and where you use it. In your case, no semicolon is needed.slotsis an macro which expands to an emtpy string, so any C++ compile can process it. However,slotsis also recognized as a special key word bymoc. The same goes forsignals, by the way (it’s a macro expanding toprotected:).