user2091592
user2091592

Reputation: 19

Undefined reference to `vtable

I am creating a GUI application using Qt; I try to do hello world using Qt and it works perfectly, but when I create custom list widget I get undefined reference to vtable error when I compile it:

I am using eclipse and c++

#ifndef QMENUFILTER_H_
#define QMENUFILTER_H_
#include <qmenu.h>
class CustomMenuFilter : QMenu
{
    Q_OBJECT
public:
    CustomMenuFilter () ;
    ~CustomMenuFilter() ;
private:
    QMenu FilterMenu;
    QAction *AddFilterAct ;
    QAction *DeleteFilterAct ;

     Q_SLOT
      void contextMenuEvent(QContextMenuEvent *event);
};
#endif /* QMENUFILTER_H_ */


#include "QMenuFilter.h"
CustomMenuFilter::CustomMenuFilter():QMenu()
{
    DeleteFilterAct = new QAction("DeleteFilter" , this);
    AddFilterAct = new QAction("AddFilter" , this);
    AddFilterAct->setText("AddFilter");
    DeleteFilterAct->setText("DeleteFilter");
}

LOG file : http://pastebin.com/raw.php?i=qZes6bkm

Upvotes: 1

Views: 3930

Answers (4)

Dumisani Kunene
Dumisani Kunene

Reputation: 729

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: 0

peppe
peppe

Reputation: 22744

From the #qt factoids, when getting a vtable error onto a QObject-derived class:

  1. Make sure the Q_OBJECT macro is present in the definition of all QObject-derived classes.
  2. Make sure you declare your QObject-derived classes in your header files ONLY.
  3. Make sure all of your header files are listed in your .pro file in the HEADERS list.
  4. Run qmake every time you add Q_OBJECT to one of your classes or modify your .pro file.

I don't see moc's output linked in the final executable in your build log:

g++ -L/usr/local/lib/ -lQtGui -lQtCore -o "CameraManagerAfterBeta" ./trunk/Source/Camera.o ./trunk/Source/Interface.o ./trunk/Source/Manager.o ./trunk/Source/QMenuFilter.o ./trunk/Source/main.o -lopencv_core -lopencv_objdetect -lopencv_video -lopencv_highgui -lopencv_imgproc

No moc_QMenuFilter.o is being linked in. That's the error. It's likely that you added the Q_OBJECT macro and forgot to re-run qmake (or didn't even add the header in the HEADERS list in the .pro file).

Upvotes: 1

Daniel Frey
Daniel Frey

Reputation: 56863

You need to define the dtor as well, which will create the vtable

CustomMenuFilter::~CustomMenuFilter() { ... }

While there are some (compiler-dependent) rules that describe when exactly the compiler emits a vtable, it's usually not important for you to know. The important thing is, that you need to define the dtor and the compiler will take care of the vtable then, so if you see the error "undefined reference to vtable", always check the dtor.

Upvotes: 5

justin
justin

Reputation: 104698

Several compilers emit the vtable in the TU which defines the first out of line definition of a virtual -- implicitly, that is your destructor in this case (because QMenu's destructor is likely virtual).

So adding your destructor's definition should fix it.

// CustomMenuFilter.cpp

CustomMenuFilter::~CustomMenuFilter() {}

Upvotes: 2

Related Questions