Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. C++ / Qt5.1.1 - Call virtual function in another class

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
virtual
10 Posts 6 Posters 4.7k Views
  • 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.
  • M Offline
    M Offline
    mad-hatter
    wrote on 30 Mar 2018, 13:44 last edited by
    #1

    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 to vtable for derived'
    path\generated\plugin\gear\obj\divideoptions.o:-1: In function ZN16LC_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;
    
    R 1 Reply Last reply 30 Mar 2018, 13:52
    0
    • M mad-hatter
      30 Mar 2018, 13:44

      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 to vtable for derived'
      path\generated\plugin\gear\obj\divideoptions.o:-1: In function ZN16LC_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;
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 30 Mar 2018, 13:52 last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mad-hatter
        wrote on 30 Mar 2018, 13:54 last edited by
        #3

        Thanks for your reply.

        Yes.
        Ran clean, qmake & rebuild

        A 1 Reply Last reply 30 Mar 2018, 15:22
        0
        • M mad-hatter
          30 Mar 2018, 13:54

          Thanks for your reply.

          Yes.
          Ran clean, qmake & rebuild

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 30 Mar 2018, 15:22 last edited by
          #4

          @mad-hatter

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

          Qt has to stay free or it will die.

          M 1 Reply Last reply 30 Mar 2018, 17:08
          0
          • A aha_1980
            30 Mar 2018, 15:22

            @mad-hatter

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

            M Offline
            M Offline
            mad-hatter
            wrote on 30 Mar 2018, 17:08 last edited by
            #5

            @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.

            A 1 Reply Last reply 30 Mar 2018, 19:38
            0
            • M mad-hatter
              30 Mar 2018, 17:08

              @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.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 30 Mar 2018, 19:38 last edited by
              #6

              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

              Qt has to stay free or it will die.

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 30 Mar 2018, 20:06 last edited by
                #7

                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 ?

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

                M 1 Reply Last reply 2 Apr 2018, 10:43
                2
                • S SGaist
                  30 Mar 2018, 20:06

                  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 ?

                  M Offline
                  M Offline
                  mad-hatter
                  wrote on 2 Apr 2018, 10:43 last edited by
                  #8

                  @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;
                  
                  J J 2 Replies Last reply 2 Apr 2018, 14:20
                  0
                  • M mad-hatter
                    2 Apr 2018, 10:43

                    @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;
                    
                    J Online
                    J Online
                    JonB
                    wrote on 2 Apr 2018, 14:20 last edited by JonB 4 Feb 2018, 14:22
                    #9

                    @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!

                    1 Reply Last reply
                    3
                    • M mad-hatter
                      2 Apr 2018, 10:43

                      @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;
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 3 Apr 2018, 05:17 last edited by jsulm 4 Mar 2018, 05:18
                      #10

                      @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/

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3

                      10/10

                      3 Apr 2018, 05:17

                      • Login

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