C++ / Qt5.1.1 - Call virtual function in another class



  • Windows 10 - GCC 5.3
    Qt Creator 4.5.2
    Based on Qt 5.10.1 (MSVC 2015, 32 bit)

    Hello,

    I'm getting the following compile errors.
    path\generated\plugin\gear\obj\divideoptions.o:-1: In function ZN7derivedC4Ev': error: undefined reference tovtable for derived'
    path\generated\plugin\gear\obj\divideoptions.o:-1: In function ZN16LC_DivideOptions8execCommEP18Document_InterfaceP7QWidget7QString': error: undefined reference toderived::getData(QHash<int, QVariant>*)'

    Help please;

    Regards

    class Plug_Entity
    {
    public:
        virtual ~Plug_Entity() {}
    
        //! Obtain the entity data.
        /*!
        * The data is a QHash with the EDATA keys relevant to the type of entity
        *  \param data pointer to a QHash<int, QVariant> to store the entity data.
        */
        virtual void getData(QHash<int, QVariant> *data) = 0;
    	...
    	...
    };
    
    class derived : public Plug_Entity //in document_interface.h
    {
        Q_OBJECT
    public:
        virtual void getData(QHash<int, QVariant> *data);
        virtual void updateData(QHash<int, QVariant> *data);
        virtual void getPolylineData(QList<Plug_VertexData> *data);
        virtual void updatePolylineData(QList<Plug_VertexData> *data);
        virtual void move(QPointF offset);
        virtual void moveRotate(QPointF const& offset, QPointF const& center, double angle);
        virtual void rotate(QPointF center, double angle);
        virtual void scale(QPointF center, QPointF factor);
        virtual QString intColor2str(int color);
    };
    
    QHash<int, QVariant> *data;
        auto obj = new derived ;
        obj->getData(data);
        qDebug() << data;
        delete obj;
    

  • Moderators

    @mad-hatter
    did you already try a full rebuild?



  • Thanks for your reply.

    Yes.
    Ran clean, qmake & rebuild


  • Qt Champions 2017

    @mad-hatter

    might be a silly question, but do you link against this object?



  • @aha_1980 - Thanks for your reply.

    This is the first time I've tried to call a virtual function (I don't know if I'm doing it correctly).
    I don't know what you mean by linking against this object.


  • Qt Champions 2017

    hi @mad-hatter,

    'undefined reference' means the linker cannot find a symbol.

    therefore my question, if you link against the compiled object file containing your class.

    to recreate the error, a minimal compilable example would be nice.

    Regards


  • Lifetime Qt Champion

    Hi,

    Usually the vtable error comes from add/removing the Q_OBJECT macro in your QObject derived class without re-running qmake. Is that your case ?



  • @SGaist

    Hello,

    Ran qmake and rebuild all.
    I'm still getting errors.

    I don't know what to do about "virtual ~Plug_Entity() {}".

    I'm making an error, but don't know what.

    Regards

    *document_interface.h*
    class Plug_Entity
    {
    public:
        virtual ~Plug_Entity() {}
        virtual void getData(QHash<int, QVariant> *data) = 0;
        virtual void updateData(QHash<int, QVariant> *data) = 0;
        virtual void getPolylineData(QList<Plug_VertexData> *data) = 0;
        virtual void updatePolylineData(QList<Plug_VertexData> *data) = 0;
        virtual void move(QPointF offset) = 0;
    	virtual void moveRotate(QPointF const& offset, QPointF const& center, double angle)=0;
        virtual void rotate(QPointF center, double angle) = 0;
        virtual void scale(QPointF center, QPointF factor) = 0;
        virtual QString intColor2str(int color) = 0;
    };
    
    class base : public Plug_Entity //in document_interface.h
    {
    public:
        virtual void getData(QHash<int, QVariant> *data);
        virtual void updateData(QHash<int, QVariant> *data);
        virtual void getPolylineData(QList<Plug_VertexData> *data);
        virtual void updatePolylineData(QList<Plug_VertexData> *data);
        virtual void move(QPointF offset);
        virtual void moveRotate(QPointF const& offset, QPointF const& center, double angle);
        virtual void rotate(QPointF center, double angle);
        virtual void scale(QPointF center, QPointF factor);
        virtual QString intColor2str(int color);
        //virtual ~Plug_Entity() {} //error: declaration of '~Plug_Entity' as member of 'base'
    };
    
    class derived : public base
    {
    public:
        void getData(QHash<int, QVariant> *data);
        void updateData(QHash<int, QVariant> *data);
        void getPolylineData(QList<Plug_VertexData> *data);
        void updatePolylineData(QList<Plug_VertexData> *data);
        void move(QPointF offset);
        void moveRotate(QPointF const& offset, QPointF const& center, double angle);
        void rotate(QPointF center, double angle);
        void scale(QPointF center, QPointF factor);
        QString intColor2str(int color);
        //~Plug_Entity() {} //error: declaration of '~Plug_Entity' as member of 'derived'
    };
    
    *Code:-*
        QHash<int, QVariant> *data;
    	
        base *p;
        derived obj1; //error: undefined reference to `vtable for derived'
        p = &obj1;
    
        p->getData(data); //error: undefined reference to `derived::getData(QHash<int, QVariant>*)'
        qDebug() << data;
    


  • @mad-hatter

    I don't know what to do about "virtual ~Plug_Entity() {}".

    It looks like you want all your methods in Plug_Entity to be pure virtual, including the destructor. So in Plug_Entity maybe you want virtual ~Plug_Entity() = 0;, and in base you (kind of) meant virtual ~Plug_Entity() {} ?

    But, in your derived classes (base & derived), you (must not) try to define a destructor for class Plug_Entity, you must define it for the class you are in! So base will have:

    virtual ~base() {}
    

    and derived will have:

    virtual ~derived() {}
    

    That is what the error message is telling you!


  • Moderators

    @mad-hatter You can't define a destructor of a base class in a derived class! So, adding ~Plug_Entity() {} in derived is invalid. Simply add ~derived() {}.
    See https://www.geeksforgeeks.org/pure-virtual-destructor-c/


Log in to reply
 

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