1

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

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
user2091592
  • 19
  • 1
  • 4
  • 1
    Why do people comment instead of answering ? (comment-answer removed in the meantime) – Patrick B. Mar 09 '13 at 10:53
  • @PatrickB.: Just yesterday I posted a small thing as an answer and got some very negative feedback that it should be a comment. Still trying to find the line here. Maybe it's "a link is not OK" vs. "a trivial piece of code is OK". – Daniel Frey Mar 09 '13 at 10:58

4 Answers4

5

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.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
2

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() {}
justin
  • 104,054
  • 14
  • 179
  • 226
1

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).

peppe
  • 21,934
  • 4
  • 55
  • 70
0

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)
Dumisani Kunene
  • 659
  • 7
  • 9