Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Undefined reference to 'vtable for myClass'
Qt 6.12 LTS is out! See what's new in the release blog

Undefined reference to 'vtable for myClass'

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 16.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT

    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);
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile&#40;QLatin1String("qml/test1/main.qml"&#41;);
    viewer.showExpanded();
    
    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
    DEPLOYMENTFOLDERS = folder_01

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

    QML_IMPORT_PATH =

    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)

    DEFINES += QMLJSDEBUGGER

    If your application uses the Qt Mobility libraries, uncomment

    the following lines and add the respective components to the

    MOBILITY variable.

    CONFIG += mobility

    MOBILITY +=

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

    SOURCES += main.cpp
    myclass.cpp

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

    include(qmlapplicationviewer/qmlapplicationviewer.pri)
    qtcAddDeployment()

    HEADERS +=
    myclass.h
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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 to vtable for myClass'
        debug/main.o: In function ~myClass': C:\Laptop\Dropbox\Qt\examples\test1-build-desktop/../test1//myclass.h:7: undefined reference to vtable 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'
        @

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yshurik
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            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)

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Hi Guys,

              Thanks for your help, I have moc and I have added
              @
              HEADERS +=
              myclass.h
              SOURCES += main.cpp
              myclass.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. =)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved