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 functionZN7derivedC4Ev': error: undefined reference to
vtable for derived'
path\generated\plugin\gear\obj\divideoptions.o:-1: In functionZN16LC_DivideOptions8execCommEP18Document_InterfaceP7QWidget7QString': error: undefined reference to
derived::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;
-
@mad-hatter
did you already try a full rebuild? -
Thanks for your reply.
Yes.
Ran clean, qmake & rebuild -
might be a silly question, but do you link against this object?
-
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
-
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 ? -
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;
-
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 inPlug_Entity
maybe you wantvirtual ~Plug_Entity() = 0;
, and inbase
you (kind of) meantvirtual ~Plug_Entity() {}
?But, in your derived classes (
base
&derived
), you (must not) try to define a destructor for classPlug_Entity
, you must define it for the class you are in! Sobase
will have:virtual ~base() {}
and
derived
will have:virtual ~derived() {}
That is what the error message is telling you!
-
@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/