2

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:

  1. 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.

  2. 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?

Community
  • 1
  • 1
Max
  • 3,824
  • 8
  • 41
  • 62
  • Macro is just a text substitution, so it may contain semicolon and you don't have to write it. – KCH Jan 11 '13 at 09:48
  • In fact the *gcc* compiler doesn't understand `public slots` (for him it's just `public`), but Qt's *moc* compiler does. Which is why you need to run that before compilation with *gcc* and also compile the generated *moc_....cpp* with *gcc* afterwards. And in the end it's the reason why a simple *gcc* call isn't eneough for a Qt program (and will likely result in compiler errors) and you should set up a proper *qmake* project. – Christian Rau Jan 11 '13 at 10:45

5 Answers5

3

You didn't give the exact error message, but I suspect what's happening is that you didn't run moc on 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:

  1. 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.

  2. slots is an macro which expands to an emtpy string, so any C++ compile can process it. However, slots is also recognized as a special key word by moc. The same goes for signals, by the way (it's a macro expanding to protected:).

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • Thanks for the explanation about macros. Now, what I did was to follow this video: http://www.youtube.com/watch?v=pHc7siV2Ads But instead of breaking into class with seperated header and source, I combine all in one file. Anyway, I've tried running moc (qmake -project) and rebuilt the project again, now I have hundreds of errors more. – Max Jan 11 '13 at 10:02
3

This is just because you didn't run qmake since you aded Q_OBJECT. Just run qmake (if you use QtCreator, it must be in the Build Menu) and then compile ;).

Hope it helped

excalibur1491
  • 1,201
  • 2
  • 14
  • 29
0

Usually an undefined reference to vtable indicates that you declared some virtual functions, but never provide the definition to them. Perhaps Q_OBJECT is declaring something?

Macros are expanded before C++ syntax is considered, working in textual form. That is why macros themselves do not have to obey C++ syntax. If, for example, Q_OBJECT contains a semicolon at the end of its definition, so that after the substitution you get a correct C++ code, then that is good enough.

slots may be a macro as well (maybe even an empty one). Then, after substituting slots with nothingness you get a valid C++ code again.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
0

You have to use the Meta Object Compiler delivered by QT

Hennaldo
  • 155
  • 6
0

In general if you're getting an undefined reference to vtable error it's because qmake hasn't ran and generated a necessary moc for it. Rerunning qmake in the project directory should fix it, if it doesn't then clean the build and run quake and make again.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55