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. Problem with "undefined reference to 'vtable for myclass' "

Problem with "undefined reference to 'vtable for myclass' "

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 10.0k 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.
  • J Offline
    J Offline
    jefazo92
    wrote on last edited by
    #1

    Hi everyone,

    I've been trying to link 2 files but I'm getting the error "undefined reference to 'vtable for myclass' ". One cpp contains my QMainWindow whereas my the other one contains my binary tree.
    The idea is that using SIGNALS and SLOTS I can use the application window to store the values into my tree.
    The application was running before but for some reason once I linked both headers and files, it stopped working.
    I'm getting errors in the constructors and destructors of my trees.
    I'm getting the error " Undefined reference to "vtable for Tree" ".
    Any help in solving this issue would be appreciated. Thanks.

    #ifndef TREE_H
    #define TREE_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QWidget>
    
    using namespace std;
    
    struct tree_node { //Create Node "template".
        tree_node *left;   // left subtree has smaller elements
        tree_node *right;  // right subtree has larger elements
        QString word; // This tree stores int values
        unsigned int count = 0; //Count for duplicates.
    };
    
    typedef tree_node* NodePtr;
    
    class Tree : public QObject 
    {
        Q_OBJECT 
    private:
        NodePtr fRoot; //Create Root of Binary Tree
        int size;
    
    
    public:
        explicit Tree(QObject *parent = 0); 
        ~Tree();
        NodePtr &getRoot() { return fRoot; } 
        void removeNode (NodePtr &ptr);
    
    
    public slots:
        void addverbose(NodePtr &ptr, QString aword);
        void addsilent(NodePtr &ptr, QString aword);
        void disp_inord(NodePtr ptr);
        void disp_inreverse(NodePtr ptr);
      
        
    
    };
    
    #endif // TREE_H
    
    
    
    
    -------------------------------------------------Tree cpp file
    
    #include "tree.h"
    #include <iostream>
    #include <QString>
    
    using namespace std;
    
    Tree::Tree(QObject *parent) : QObject(parent)
    {
        fRoot = NULL; // Initialize pointer (make root node to point to Null).
      
    }
    
    Tree::~Tree()
    {
      removeNode(fRoot);
    }
    
    void Tree::disp_inord(NodePtr ptr) //Display contents of dictionary
    {
        if (ptr != NULL)
        {
            disp_inord(ptr->left);       // print left subtree
            std::cout << qPrintable(ptr->word) << '\n';    // print this node
            disp_inord(ptr->right);      // print right subtree
        }
    }
    
    void Tree::disp_inreverse(NodePtr ptr) //Display contents of dictionary in reverse order
    {
        if (ptr != NULL)
        {
            disp_inreverse(ptr->right);       // print right subtree
            std::cout << qPrintable(ptr->word) << '\n';    // print this node
            disp_inreverse(ptr->left);      // print left subtree
        }
    }
    
    
    /*auto new_end = std::remove_if(s.begin(), s.end(), [](QChar c) { return c.isPunct(); });
    s.resize(new_end - s.begin());  CODE TO REMOVE CHARACTERS WHICH AREN'T LETTERS OR NUMBERS.*/
    
    
    
    
    
    
    void Tree::addverbose(NodePtr &ptr, QString aword){  //Why ptr passed as a reference here ????????????????????????????????????????
    
    
        
    }
    
    void Tree::addsilent(NodePtr &ptr, QString aword){  //Why ptr passed as a reference here ????????????????????????????????????????
    
    
     
        }
    
    
    
    
    
    void Tree::removeNode(NodePtr &ptr){
        if(ptr != NULL)
        {
    
                removeNode (ptr->left);
    
                removeNode (ptr->right);
    
            delete ptr;
    
        }
    }
    
    KillerSmathK 1 Reply Last reply
    0
    • J jefazo92

      Hi everyone,

      I've been trying to link 2 files but I'm getting the error "undefined reference to 'vtable for myclass' ". One cpp contains my QMainWindow whereas my the other one contains my binary tree.
      The idea is that using SIGNALS and SLOTS I can use the application window to store the values into my tree.
      The application was running before but for some reason once I linked both headers and files, it stopped working.
      I'm getting errors in the constructors and destructors of my trees.
      I'm getting the error " Undefined reference to "vtable for Tree" ".
      Any help in solving this issue would be appreciated. Thanks.

      #ifndef TREE_H
      #define TREE_H
      
      #include <QMainWindow>
      #include <QObject>
      #include <QWidget>
      
      using namespace std;
      
      struct tree_node { //Create Node "template".
          tree_node *left;   // left subtree has smaller elements
          tree_node *right;  // right subtree has larger elements
          QString word; // This tree stores int values
          unsigned int count = 0; //Count for duplicates.
      };
      
      typedef tree_node* NodePtr;
      
      class Tree : public QObject 
      {
          Q_OBJECT 
      private:
          NodePtr fRoot; //Create Root of Binary Tree
          int size;
      
      
      public:
          explicit Tree(QObject *parent = 0); 
          ~Tree();
          NodePtr &getRoot() { return fRoot; } 
          void removeNode (NodePtr &ptr);
      
      
      public slots:
          void addverbose(NodePtr &ptr, QString aword);
          void addsilent(NodePtr &ptr, QString aword);
          void disp_inord(NodePtr ptr);
          void disp_inreverse(NodePtr ptr);
        
          
      
      };
      
      #endif // TREE_H
      
      
      
      
      -------------------------------------------------Tree cpp file
      
      #include "tree.h"
      #include <iostream>
      #include <QString>
      
      using namespace std;
      
      Tree::Tree(QObject *parent) : QObject(parent)
      {
          fRoot = NULL; // Initialize pointer (make root node to point to Null).
        
      }
      
      Tree::~Tree()
      {
        removeNode(fRoot);
      }
      
      void Tree::disp_inord(NodePtr ptr) //Display contents of dictionary
      {
          if (ptr != NULL)
          {
              disp_inord(ptr->left);       // print left subtree
              std::cout << qPrintable(ptr->word) << '\n';    // print this node
              disp_inord(ptr->right);      // print right subtree
          }
      }
      
      void Tree::disp_inreverse(NodePtr ptr) //Display contents of dictionary in reverse order
      {
          if (ptr != NULL)
          {
              disp_inreverse(ptr->right);       // print right subtree
              std::cout << qPrintable(ptr->word) << '\n';    // print this node
              disp_inreverse(ptr->left);      // print left subtree
          }
      }
      
      
      /*auto new_end = std::remove_if(s.begin(), s.end(), [](QChar c) { return c.isPunct(); });
      s.resize(new_end - s.begin());  CODE TO REMOVE CHARACTERS WHICH AREN'T LETTERS OR NUMBERS.*/
      
      
      
      
      
      
      void Tree::addverbose(NodePtr &ptr, QString aword){  //Why ptr passed as a reference here ????????????????????????????????????????
      
      
          
      }
      
      void Tree::addsilent(NodePtr &ptr, QString aword){  //Why ptr passed as a reference here ????????????????????????????????????????
      
      
       
          }
      
      
      
      
      
      void Tree::removeNode(NodePtr &ptr){
          if(ptr != NULL)
          {
      
                  removeNode (ptr->left);
      
                  removeNode (ptr->right);
      
              delete ptr;
      
          }
      }
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @jefazo92
      Try to clear the build generated files and rebuild your project.
      0_1556837460052_clearfiles.gif

      So, i noticed you are using recursive calls to access the element of your tree, but where is the stop condition ?

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      2
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        Rerun qmake, that was my first inclination, but I saw a reference to this here:
        https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable

        Might want to do a make clean as well. In case something else is stupid.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        1
        • J Offline
          J Offline
          jefazo92
          wrote on last edited by
          #4

          It didn't work. I also tried doing virtual ~Tree(); but I got "virtual Tree::~Tree' cannot be overloaded."
          I also get with " 'virtual Tree::~Tree'. " and "declaration of " ~Tree " as non member".

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            What version of Qt are you using ?
            What compiler are you running ?

            I just tried to build your tree class on macOS and got no problem.

            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
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Check if you have a cyclic includes.
              like tree.h in mainwindow.h and mainwindow.h in tree.h
              That gives such undefined linker errors.

              1 Reply Last reply
              1

              • Login

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