Undefined link to vtable. Trying to compile a Qt project

I am using Code :: Blocks 8.02 and the mingw 5.1.6 compiler. I get this error when compiling my Qt project:

C: \ Documents and Settings \ Fuzz \ Desktop \ GUI \ App_interface.cpp | 33 | undefined link 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 

AddressBook.cpp File:

 #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")); } 
+51
c ++ qt qmake
Oct 11 '09 at 23:25
source share
17 answers

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

To automatically ensure that all mc cpp files are generated, you can force qmake to automatically generate a .pro file for you, rather than writing it yourself.

Run

 qmake -project 

in the project directory, and qmake scans your directory for all C ++ headers and source files to generate mc cpp files for.

+43
Oct 12 '09 at 4:51
source share

When using Qt Creator:

  • Build -> Run qmake
  • Build β†’ Restore All
+47
Jul 14 '10 at 17:21
source share

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

To answer in a little more detail, the Q_OBJECT macro displays the Qt moc tool to create an additional implementation file that contains the code needed to support the QObject meta-information system. If you had any signals or slots, this would also do something for them.

An alternative solution would be to delete the Q_OBJECT macro. You probably don't want to do this, but it will help the immediate problem, and it is not strictly necessary for the code that you presented.

In addition, I would like to note that your line:

 #include "addressbook.h" 

Must be:

 #include "addressbook.h" 

based on how you provided the file names in the question.

+16
Oct 12 '09 at 0:53
source share

Assuming you are using qmake to create your Makefile, make sure AddressBook.h is listed in your HEADERS variable in the .pro file, for example.

 HEADERS = AddressBook.h 
+9
Oct 12 '09 at 2:18
source share

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

 virtual void process(); 

gave this error while

 virtual void process() = 0; 

made it go away.

For those who google this problem, make sure that all virtual functions are defined. (via "= 0" or the full definition in the source file) I am using Netbeans with the MinGW compiler.

+4
Apr 02 2018-11-11T00:
source share

For CMake projects, set CMAKE_AUTOMOC to ON, this solved 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) 
+3
Nov 15 '17 at 13:28
source share

I had the same problem, but as soon as I defined my constructor in the header file instead of .cpp, the error went away. In addition, the corresponding moc file was absent in the file system and in the Makefile section "compiler_moc_header_make_all". I started qmake and then finally everything was built with success. I went to check the Makefile and it was there now.

+2
Dec 02 '09 at 19:54
source share

deleted the build folder, restarted Qt Creator and worked

+2
Mar 29 '11 at 12:51
source share

I come to the same problem, rebuild the project, never update the Makefile, I delete the Makefile and rebuild, the problem is gone. ps: run 'make' from the command line can provide you with more details than the IDE, and is useful for getting the real problem.

+1
Feb 26 '10 at 2:55
source share

One reason is that you declare virtual functions in the class and do not define their body.

+1
Nov 07 2018-11-11T00:
source share

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

+1
Apr 19 '15 at 20:58
source share

Go to the .pro file and make sure that the .h file has an β€œinclude” in front of it. HEADERS + = include / file.h \ include / file2.h

0
Aug 10 '10 at 18:22
source share

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

  • Change void process(); on void process() = 0;
  • Creating process() public instead of private
0
Oct 06
source share

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

0
Nov 08 '17 at 9:29
source share

Just Run qmake for your project. This is easy to do by right-clicking on the name of your project and selecting Run qmake .

0
Jan 08 '19 at 5:18
source share

The header files for compiling moc must be contained in the variable HEADERS + = ...:

I moved the header files in Myproject.pro to the SOURCES + = ... section because I want mySource.h and mySource.cpp to be in the same tree element. But this is a mistake for QT Creator. As a result, an "Undefined vtable reference" error occurred. It seems: QT only finds the header for compiling moc in the HEADERS + = ... section (or variable). See also the correct explanation in https://stackoverflow.com/a/2129609/ ... Second answer: "I have seen many ways to solve the problem, but there is no explanation why this is happening, so here it is." In my opinion, this is an accurate explanation of the problem that helped me find and solve my problem.

0
Aug 15 '19 at 8:58
source share

I use the Qt creator to compile and run my programs, I often do not use the Qt command line . One thing I did to get rid of the annoying " vtable something something " error by adding the following lines to the .pro file.

TEMPLATE = application

QT + = core

-one
Sep 19 '14 at 3:54 on
source share



All Articles