Question

I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:

C:\Documents and Settings\The Fuzz\Desktop\GUI\App_interface.cpp|33|undefined reference to `vtable for AddressBook'

File AddressBook.h:

 #ifndef ADDRESSBOOK_H
 #define ADDRESSBOOK_H

 #include <QWidget>

 class QLabel;
 class QLineEdit;
 class QTextEdit;

 class AddressBook : public QWidget
 {
     Q_OBJECT

 public:
     AddressBook(QWidget *parent = 0);

 private:
     QLineEdit *nameLine;
     QTextEdit *addressText;
 };

 #endif

File AddressBook.cpp:

#include <QtGui>
#include "addressbook.h"

AddressBook::AddressBook(QWidget *parent)
     : QWidget(parent)
{
    QLabel *nameLabel = new QLabel(tr("Name:"));
    nameLine = new QLineEdit;

    QLabel *addressLabel = new QLabel(tr("Address:"));
    addressText = new QTextEdit;

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(nameLabel, 0, 0);
    mainLayout->addWidget(nameLine, 0, 1);
    mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
    mainLayout->addWidget(addressText, 1, 1);

    setLayout(mainLayout);
    setWindowTitle(tr("Simple Address Book"));
}
Was it helpful?

Solution

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.

OTHER TIPS

When using Qt Creator:

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

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

I got this while using pure virtual functions. For example,

virtual void process();

gave this error, while

virtual void process() = 0;

made it go away.

For anyone who's Googling this problem, check that all virtual functions are defined. (via " = 0" or a full definition in the source file) I'm using Netbeans with the MinGW compiler.

For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.

#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.

deleted the build folder, restarted Qt Creator and it worked

I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone. ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.

One cause is when you declare a virtual functions in a class and you don't define their body.

In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!

Go to .pro file and make sure .h file has 'include' before it. HEADERS += include/file.h \ include/file2.h

I had the same problem trying to use a protected virtual function. Two things worked.

  1. Changing void process(); to void process() = 0;
  2. Making process() public instead of private

You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.

Simply Run qmake for your project. This can easily be done by right-clicking on the name of your project and clicking on Run qmake.

Header files for moc compilation should contained in the HEADERS += ... variable:

I have moved the header files in the Myproject.pro to the SOURCES += ... section, because I want to have mySource.h and mySource.cpp in the same tree element. But that is faulty for QT Creator. In result the error "Undefined reference to vtable" has occured. It seems to be: QT detects header for moc compilation only in the HEADERS +=... section (or variable). See also the correct explaination in the other stackoverflow answer Second anwer "I've seen a lot of ways to solve the problem, but no explanation for why it happens, so here goes.". In my mind this is an exactly explaination of the problem, which has help me to found and solve my problem.

I am using Qt creator to compile and run my programs, I don't use Qt command prompt often. One thing I did to get rid of the annoying error "vtable something something" is by adding the following lines to .pro file.

TEMPLATE = app

QT += core

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top