Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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;
    
        }
    }
    


  • @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 ?



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



  • 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".


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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.


Log in to reply