Undefined reference to 'vtable for myClass'

  • I have this undefined reference to 'vtable for myClass' problem. Why is it so?

    I have a header file myClass.h which contains a slot function

    #ifndef MYCLASS_H
    #define MYCLASS_H
    #include <QObject>
    #include "qdebug.h"

    class myClass : public QObject

    public slots:
    void cppSlot(int number);


    #endif // MYCLASS_H

    A cpp file myClass.cpp

    #include "myclass.h"

    void myClass::cppSlot(int number) {
    qDebug() << "Called the C++ slot with" << number;

    with main file main.cpp

    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "QDeclarativeContext"
    #include "myclass.h"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    QmlApplicationViewer viewer;
    myClass MyClass;
    viewer.rootContext()->setContextProperty("myObject", &MyClass);
    return app.exec();


    I have also spcific the header and cpp file in my .pro file.


    Add more folders to ship with the application, here

    folder_01.source = qml/test1
    folder_01.target = qml

    Additional import path used to resolve QML modules in Creator's code model


    symbian:TARGET.UID3 = 0xE6EDC966

    Allow network access on Symbian

    symbian:TARGET.CAPABILITY += NetworkServices

    Define QMLJSDEBUGGER to allow debugging of QML in debug builds

    (This might significantly increase build time)


    If your application uses the Qt Mobility libraries, uncomment

    the following lines and add the respective components to the

    MOBILITY variable.

    CONFIG += mobility


    The .cpp file which was generated for your project. Feel free to hack it.

    SOURCES += main.cpp

    Please do not modify the following two lines. Required for deployment.


    HEADERS +=

  • Moderators

    it would be good to see the complete error message as well.

  • Hi koahnig,

    The error message is as follows:

    debug/main.o: In function myClass': C:\Laptop\Dropbox\Qt\examples\test1-build-desktop/../test1//myclass.h:7: undefined reference tovtable for myClass'
    debug/main.o: In function ~myClass': C:\Laptop\Dropbox\Qt\examples\test1-build-desktop/../test1//myclass.h:7: undefined reference tovtable for myClass'
    collect2: ld returned 1 exit status
    mingw32-make[1]: *** [debug\test1.exe] Error 1
    mingw32-make: *** [debug] Error 2
    The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
    Error while building project test1 (target: Desktop)
    When executing build step 'Make'

  • you may need to qmake+rebuild, also you may try to define at least virtual destructor

  • http://doc.qt.nokia.com/4.7/debug.html#common-bugs

    (moc wasn't run on your header, and/or the resulting cpp wasn't linked in. rerun qmake)

  • Hi Guys,

    Thanks for your help, I have moc and I have added
    HEADERS +=
    SOURCES += main.cpp

    in my .pro file. I found my mistake, I must also add

    CONFIG += qt

    It works now, even though I don't know why. =)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.