Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 'undefined reference to vtable' when adding Q_OBJECT
Forum Updated to NodeBB v4.3 + New Features

'undefined reference to vtable' when adding Q_OBJECT

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 2.6k 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.
  • K Offline
    K Offline
    kitfox
    wrote on 15 May 2019, 21:21 last edited by aha_1980
    #1

    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;
    };
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 May 2019, 21:24 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4

      1/2

      15 May 2019, 21:21

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved