c++ - 创建树节点时 vtable 的 undefined reference

标签 c++ binary-tree vtable

<分区>

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

表达式 Node.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();        

}; 

然后我遇到问题的代码来 self 自己的函数 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 已声明但未定义。只需定义函数的主体,它应该可以很好地链接...

关于c++ - 创建树节点时 vtable 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17754802/

相关文章:

c++ - 初始化指向同名函数的静态成员函数指针

algorithm - 计算深度的二叉树叶子

c++ - 虚拟表未定义

java - 在 C++ 中,我们可以向上转换一个数组,然后尝试将另一个子类型放入其中(受到 Java ArrayStoreException 的启发)吗?

c++ - 从作为分离线程运行的 boost::asio::io_service::work 捕获异常

c++ - 每个任务一个异步线程?

c++ - 如何处理错误的数据类型输入

data-structures - 时间序列数据的最佳数据结构

rust - 无法在Rust中对二叉树进行广度优先搜索

c++ - 在运行时添加字段 : suffer either vtable-cost or cache miss at destructor