TheFuzz
TheFuzz

Reputation: 2623

Undefined reference to vtable. Trying to compile a Qt project

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"));
}

Upvotes: 56

Views: 94287

Answers (22)

DanRechtsaf
DanRechtsaf

Reputation: 578

Bit late to the party, but here might be another reason for this error (and solution):
If you are using cmake, make sure to add the include file with the QT object ( AddressBook.h in the question) to the target-sources: eg at add_executable.
Else the MOC will not process that include file. (if it is header-only).
(Note: normally for include files it is not needed to add them to the cmake target, so this mistake is easy to make)

Upvotes: 0

YU Chen  Shih
YU Chen Shih

Reputation: 75

the most errors from using Q_OBJECT but not generate moc file
the moc file define
classname::metaObject
staticMetaObject
qt_metacall

Here is a example:
when source or header using Q_OBJECT

/opt/qt/bin/moc ./window.h -o ./moc_window.cpp

Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o

try this source
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz

refer to http://fatalfeel.blogspot.com/2013/11/qt5-c-building.html

Upvotes: 0

Goran Shekerov
Goran Shekerov

Reputation: 1

CMake

when using CMake, interestingly enough if my .cpp and .h files are not in the same folder the moc, bu default, fails to generate the meta file :)

Upvotes: 0

Felipe C.
Felipe C.

Reputation: 181

I was running into the same problem using CLion, and solved it by adding these lines to CMakeLists.txt

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

See https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion

Upvotes: 4

Hamed
Hamed

Reputation: 36

Just clear the project and then rebuild it!

Upvotes: 1

Hartmut Schorrig
Hartmut Schorrig

Reputation: 549

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.

Upvotes: 0

user688627
user688627

Reputation:

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.

Upvotes: 4

Sunit Gautam
Sunit Gautam

Reputation: 6045

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.

Upvotes: 1

Dumisani Kunene
Dumisani Kunene

Reputation: 729

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)

Upvotes: 7

Emil Fors
Emil Fors

Reputation: 110

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

Upvotes: 0

blwy10
blwy10

Reputation: 4892

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.

Upvotes: 47

Anurag Verma
Anurag Verma

Reputation: 549

When using Qt Creator:

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

Upvotes: 54

Aleksei Petrenko
Aleksei Petrenko

Reputation: 7178

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

Upvotes: 1

khafen
khafen

Reputation: 151

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

Upvotes: 1

Mmakgabo Makgoba
Mmakgabo Makgoba

Reputation: 1

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

Upvotes: -1

agus
agus

Reputation: 11

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

Upvotes: 1

yolo
yolo

Reputation: 2795

deleted the build folder, restarted Qt Creator and it worked

Upvotes: 2

Phil
Phil

Reputation: 405

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

Upvotes: 0

user281754
user281754

Reputation: 11

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.

Upvotes: 1

yan bellavance
yan bellavance

Reputation: 4830

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.

Upvotes: 2

Jeremy Friesner
Jeremy Friesner

Reputation: 73081

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

Upvotes: 10

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

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.

Upvotes: 16

Related Questions