0

I have a class extending QWidget

 class Boo : public QWidget {
      Q_OBJECT
 public:
      Boo(QWidget* parent) : QWidget(parent) {}
 };

If I just add it to cpp file I get the following error:

...[Boo::Boo(QWidget*)]+0x71): undefined reference to `vtable for Boo'

Why does it happen?

Is it possible to add the class to .cpp file anyway?

Evgeny
  • 2,121
  • 1
  • 20
  • 31
  • 2
    Check out this [question](http://stackoverflow.com/questions/5854626/qt-signals-and-slots-error-undefined-reference-to-vtable-for) – Pedro Gandola Mar 13 '14 at 08:10
  • 2
    Another possible duplicate: http://stackoverflow.com/questions/3001615/qt-moc-with-implementations-inside-of-header-files – hyde Mar 13 '14 at 08:19

2 Answers2

0

An answer is that you definitely can add such declarations in *.cpp files, there is nothing wrong with it, but you should not forget to MOC *.cpp files, so appropriate *_moc.cpp file which be generated and checked that it's included in the project. if you are using qmake you should be fine, if you are using VisualStudio you should regenerate VS project file.

Main reason is for this error is that you don't have *_moc.cpp file wither generated or included in the project.

evilruff
  • 3,947
  • 1
  • 15
  • 27
  • Do you mean I should have moc_Boo.cpp file? If yes, can I add its creation to the .pro file? – Evgeny Mar 13 '14 at 09:58
  • Exactly. If you have your cpp file declared in .pro file in sources, then it will be created automatically as well as added to makefile/solution – evilruff Mar 13 '14 at 19:42
-1

No you cannot add the class just to the .cpp file in build systems MOC'ing only header files. The class declaration needs to be located in a header file that is MOC'ed by the build system.

user2672165
  • 2,986
  • 19
  • 27