链接错误未定义vtable和typeinfo

Linking errors undefined vtable and typeinfo

本文关键字:typeinfo vtable 未定义 错误 链接      更新时间:2023-10-16

我得到这个错误:

Undefined                     first referenced
symbol                              in file
typeinfo for Operand                Expression_Tree.o
vtable for Operand                  Expression.o

通过一些谷歌搜索和类似的问题,我发现这可能是因为我如何处理中的虚拟函数(特别是构造函数/析构函数)

class Expression_Tree
{
 public:
  virtual double           evaluate() = 0;
  virtual std::string      get_postfix() const = 0;
  virtual std::string      get_infix() const = 0;
  virtual std::string      str() const = 0;
  virtual void             print(std::ostream&, int hopCount = 0) const = 0;
  virtual Expression_Tree* clone() const = 0;
  virtual ~Expression_Tree(){}
 protected:
  Expression_Tree() {}
  Expression_Tree(const Expression_Tree& o) = delete; 
  Expression_Tree(Expression_Tree&&) = default;
};

有人看到什么不同寻常的东西吗?

正如你们中的一些人在Operand类中所说,问题是它的基。

class Operand : public Expression_Tree
{
 public:
  Operand& operator=(const Operand&) = default;
  Operand& operator=(Operand&&) = default;
  // void print(std::ostream&, int hopCount = 0) const; <- this was the problem
  std::string get_postfix() const;
  std::string get_infix() const;
  ~Operand() = default;
  Operand(const Operand&) = default;
  Operand(Operand&&) = default;
 protected:
 Operand() 
   : Expression_Tree() {}
};

一旦我删除了void print(),一切都解决了=)