Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Minimal QObject example doesn't compile !



  • Why does this minimal example not compile?
    I get the dreaded 'undefined reference to vtable' error. :(

    Header:

    class MyObject: public QObject
    {
        Q_OBJECT
    
    public:
        MyObject();
        virtual ~MyObject();
    };
    

    Definition:

    MyObject::MyObject(){}
    MyObject::~MyObject(){}
    

    My .pro file is:

    QT += testlib
    QT += gui widgets
    
    CONFIG += qt console warn_on depend_includepath testcase
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES +=  \
        MyObject.cpp \
        tst_obj.cpp
    

    I'm sure you don't need to have a slot/signal if you inherit from QObject. Is something very wrong with the .pro file?!

    Although I don't think it's relevant, here's the test file:

    #include <QtTest>
    #include "MyObject.h"
    
    class TestMyObject : public QObject
    {
        Q_OBJECT
    public:
        TestMyObject();
        ~TestMyObject();
    
    private slots:
        void test_createMyObject();
    };
    
    TestMyObject::TestMyObject(){}
    TestMyObject::~TestMyObject(){}
    
    void TestMyObject::test_createMyObject()
    {    
        MyObject myObject;
    }
    QTEST_APPLESS_MAIN(TestMyObject)
    #include "tst_obj.moc"
    

  • Lifetime Qt Champion

    You forgot to add the header to HEADERS


  • Lifetime Qt Champion

    You forgot to add the header to HEADERS



  • Yes...that's it. And I thought I was doing something wrong in the class somewhere!
    Many thanks!!!


Log in to reply