1

Stuck with this weird question

Why following code is OK for g++

#include <QObject>

class B {
public:
  B(){}
  ~B(){}
};

class A : public QObject, public B {
  Q_OBJECT
public:
  A(QObject * parent = 0 ) : QObject( parent ), B() {}
  ~A(){}
};

int main(int argc, char *argv[])
{
  A a1();
  //A * a = new A();
  //delete a;
  return 0;
}

and this can not be compiled

/*... the same class definitions as above */    

int main(int argc, char *argv[])
{
  //A a1();
  A * a = new A();
  delete a;
  return 0;
}

//error: undefined reference to `vtable for A'

I mean what to do to make the second good as well?

PS Well I put everything in separate files, and it works fine. So it is a matter of Q_OBJECT macros, I think.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21

3 Answers3

3

Why does the First example compile & Link cleanly while Second doesn't?

The first example compiles and links because:
It does not create an object of A,

A a1();

Declares a function a1() which takes no parameter and returns a A type.

While the Second example creates an object when new is called.

Note that the *undefined reference to vtable for A'* is a linking error and will only be emitted when a object ofclass A` is created. Hence only the Second example shows the error.

How to resolve the problem?
You need to provide definition for all virtual functions which you derive from QObject.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    QObject is not pure virtual; there's nothing you *need* to provide a definition for. – tmpearce Mar 10 '12 at 05:55
  • @tmpearce: That is incorrect.You might want to have a look at *c++03 10.3 Virtual functions [class.virtual]* or [this](http://stackoverflow.com/questions/8642124/should-a-virtual-function-essentially-have-a-definition) answer of mine. – Alok Save Mar 10 '12 at 05:58
  • this class compiles OK, no need to implement anything but constructor `class C : public QObject { Q_OBJECT public: explicit C(QObject *parent = 0); ~C(){ std::cout << "ok c \n";} int i; signals: public slots: };` – Dmitriy Kachko Mar 10 '12 at 06:01
  • 1
    @Als Perhaps I'm misunderstanding. `QObject` provides methods for all of its virtual functions. You can override them, but there's nothing that *must* be defined, as a definition already exists. – tmpearce Mar 10 '12 at 06:06
3

If you define a QObject-derived class, build an application, and realize you forgot to add the Q_OBJECT macro, and you add it later, it is important that you qmake to explicitly update the Makefile. Furthermore, to be safe, I recommend a make clean to get rid of old files. make is not smart enough to clean up all of its generated files under such circumstances, and this is an issue that often causes headaches to new Qt developers.

For more information about this error message, see

http://cartan.cas.suffolk.edu/oopdocbook/html/commonlinkererrors.html#undefinedreftovtable

shofee
  • 2,079
  • 13
  • 30
  • Interesting link, thanks. +1. I think it is relevant to the problem. "All-inline Classes For polymorphic classes [72], there should be at least one non-inline definition (a function or a static member) in a source (.cpp) file for that header file. Without this, many linkers will not be able to find any of its virtual method definitions, and will report a similar error. All-inline classes are legal in C++, but they do not work in their intended way when mixed with polymorphism." I think this is perhaps what is going on, as the question-asker mentioned that a .cpp file helped. – tmpearce Mar 10 '12 at 06:16
  • Thank you, I get rid from my real issue (which I modelling here) from working project when add another class inherited from QObject and rebuild the project. Strange but it helps. – Dmitriy Kachko Mar 10 '12 at 06:39
0

The code works in Vis. Studio. Your problem may be that B is not a polymorphic class - I don't know why that would give you an error - but you could try making something in B virtual: virtual ~B(){} for example.

tmpearce
  • 12,523
  • 4
  • 42
  • 60