'undefined reference to vtable' when adding Q_OBJECT



  • I'm having a compiler issue I can't understand. I have two classes which inherit from a common base class which in turn inherits from QObject. I'm doing this because I'm storing the objects in a list and want to be able to distinguish them later:

    for (int i: _elements.keys())
    {
        DrawableElement* de = _elements[i];
    
        if (qobject_cast<DrawableElementContour*>(de))
        {
            DrawableElementContour* ctr = qobject_cast<DrawableElementContour*>(de);
            ...
        }
        else if (qobject_cast<DrawableElementStroke*>(de))
        {
            DrawableElementStroke* ctr = qobject_cast<DrawableElementStroke*>(de);
            ...
        }
    

    However, when I add the Q_OBJECT macro to my DrawableElementStroke class, it causes an error when I compile:

    C:/Qt/5.12.3/mingw73_32/include/QtCore/qobject.h:504: undefined reference to `Gf::DrawableElementStroke::staticMetaObject'
    debug/DrawableElementStroke.o: In function `ZN2Gf21DrawableElementStrokeC2EiP7QObject':
    D:\dev\starling2\game\2019\build-GoldfinchQt-Desktop_Qt_5_12_3_MinGW_32_bit2-Debug/../GoldfinchQt/core/DrawableElementStroke.cpp:9: undefined reference to `vtable for Gf::DrawableElementStroke'
    debug/DrawableElementStroke.o: In function `ZN2Gf21DrawableElementStrokeD2Ev':
    D:\dev\starling2\game\2019\build-GoldfinchQt-Desktop_Qt_5_12_3_MinGW_32_bit2-Debug/../GoldfinchQt/core/DrawableElementStroke.cpp:13: undefined reference to `vtable for Gf::DrawableElementStroke'
    collect2.exe: error: ld returned 1 exit status
    

    This is strange because my DrawableElementContour class has almost the same structure. If I omit the Q_OBJECT from DrawableElementStroke I can compile, but then I am unable to use the qobject_cast<>.

    These are my headers:

    DrawableElement.h

    class ScenePanel;
    
    class DrawableElement : public QObject
    {
        Q_OBJECT
    
        int _id;
    
    public:
        DrawableElement(int id, QObject* parent = nullptr);
    
        int id() { return _id; }
        virtual QRectF bounds() = 0;
        virtual void paint(QPainter&) {}
    
        virtual void toXml(QDomElement root) {}
        virtual void initFromXml(QDomElement root) {}
    
    };
    

    DrawableElementStroke.h

    class DrawableElementStroke : public DrawableElement
    {
        Q_OBJECT
    
        StrokeShape _stroke;
    
    public:
        DrawableElementStroke(int id, QObject* parent = nullptr);
        ~DrawableElementStroke() override;
    
        StrokeShape stroke() { return _stroke; }
        void setStroke(const StrokeShape& stroke) { _stroke = stroke; }
    
        QRectF bounds() override { return _stroke.bounds(); }
        void paint(QPainter& p) override;
    
        void toXml(QDomElement root) override;
        void initFromXml(QDomElement root) override;
    };
    

    DrawableElementCountour.h

    class DrawableElementContour : public DrawableElement
    {
        Q_OBJECT
    
        Shape2D _shape;
    
    public:
        DrawableElementContour(int id, QObject* parent = nullptr);
        ~DrawableElementContour() override;
    
        Shape2D shape() { return _shape; }
        void setShape(const Shape2D& shape) { _shape = shape; }
    
        QRectF bounds() override { return _shape.bounds(); }
        void paint(QPainter& p) override;
    
    
        void toXml(QDomElement root) override;
        void initFromXml(QDomElement root) override;
    };
    

  • Lifetime Qt Champion

    Hi,

    This question hasn't been asked in while but the answer is still the same: re-run qmake after you add/remove the Q_OBJECT macro.


Log in to reply
 

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