Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to vtable. Trying to compile a Qt project

Tags:

c++

qt

qmake

When using Qt Creator:

  1. Build → Run qmake
  2. Build → Rebuild All

Warning: Do not do this if you already have a .pro file - you'll lose it!

In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.

Run

qmake -project

in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.


The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc on your code before compiling, right?)

To answer a little more thoroughly, the Q_OBJECT macro signals Qt's moc tool to create an extra implementation file that contains the code necessary to support QObject's meta-information system. If you had any signals or slots, it would do a few things for those as well.

An alternative solution might be to remove the Q_OBJECT macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.

Also, I would note that your line:

#include "addressbook.h"

Should probably be:

#include "AddressBook.h"

based on how you presented the filenames in the question.


Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.

HEADERS = AddressBook.h