创建树节点时vtable的未定义引用

undefined reference for vtable when creating tree nodes

本文关键字:未定义 引用 vtable 树节点 创建      更新时间:2023-10-16

我是一名学生,正在处理一个数据结构类,在上次作业中遇到了一些麻烦。目的是使用我们教授提供的一些预定义的节点类创建一个二进制表达式树。我们提供的文件如下。

表达式节点.h

class ExpressionNode 
{
protected:
    string parent;
    ExpressionNode *left;   // The left operand.
  ExpressionNode *right;  // The right operand.
public: 
    // Returns the number value of this node.    
    virtual int getValue() = 0;  
    //returns parent node
    string getParent() {return parent;};
    //returns left child
    ExpressionNode* getLeft() { return left; }
    //returns right child
    ExpressionNode* getRight() { return right; }
};

//A subclass of ExpressionNode that represents a math node that holds a number value  
class ConstantNode : public ExpressionNode
{     
public:
    // Constructor.  Create a node to hold number.
    ConstantNode(string theNumber) 
    { 
        ExpressionNode::parent = theNumber;
        ExpressionNode::left = NULL;
        ExpressionNode::right = NULL;
    }

    //Returns the number value in the node
    int getValue();        
}; 

然后我遇到问题的代码来自我自己的函数build()

void myExpressionTree::build()
{
   post = this->postfixInput(); //creates the postfix input string to be read by function
   cout << post << endl; 
   for (int i =0; i < post.size(); i ++)
   {
     if (post[i] >= '0' && post[i] <='9' ) 
     {
     string num1;
     num1 += post[i]; 
     ConstantNode *num = new ConstantNode(num1); 
     theNodes.push(num); 
     }
     else if (post[i] == '*' || post[i] == '+' || post[i] == '-' || post[i] =='/')
     {
     do stuff...
     }
  }
}

当我尝试编译时,我得到了undefined reference to 'vtable for ConstantNode'

如果有人能指出我做错了什么,那将是一个很大的帮助。

看起来ConstantNode::getValue已声明但未定义。只要定义函数的主体,它就应该很好地链接。。。