我将如何从这个抽象类创建一个子类?Vtable 错误
How would I create a sub class from this abstract class ? Vtable Error
我只是一个试图学习C++的初学者。我正在尝试解决以下问题。我不知道是什么导致了这个问题。
这是我得到的抽象类:
#ifndef __EXPR_H__
#define __EXPR_H__
#include <string>
class Expr {
public:
virtual int eval() const = 0;
virtual std::string prettyPrint() const = 0;
virtual ~Expr();
};
#endif
我正在尝试创建此类的子类,我的.h文件如下所示
#ifndef __NUM_H__
#define __NUM_H__
#include <iostream>
#include "expr.h"
//class of lone expression
class Num:public Expr
{
private:
int operand;
public:
Num(int operand):operand(operand){}
int eval() const;
std::string prettyPrint() const;
~Num(){}
};
#endif
虽然我对 Num 类的实现看起来像这个"num.cc"
#include "num.h"
#include <sstream>
std::string Num::prettyPrint()
{
std::stringstream convert;
convert << operand;
return convert.str();
}
int Num::eval()
{
return operand;
}
我不断收到以下错误。我不知道是什么原因造成的。
Undefined symbols for architecture x86_64:
"vtable for Num", referenced from:
Num::Num(int) in rpn-dc20fb.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for Expr", referenced from:
Expr::Expr() in rpn-dc20fb.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
任何帮助将不胜感激。谢谢!
虚拟析构函数需要一个实现
virtual ~Expr() { }
编译器尝试在给定虚拟(纯或非)析构函数的情况下构建虚拟表,并且由于找不到实现而抱怨。
除了由P0W指定之外,函数"eval"和"prettyPrint"还需要在"num.cc"中定义为"const"
std::string Num::prettyPrint() const
{
std::stringstream convert;
convert << operand;
return convert.str();
}
int Num::eval() const
{
return operand;
}
相关文章:
- 将父类的子类的数据复制到具有相同父类的另一个类
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 创建子类的多个实例,其中只有一个超类实例
- 创建一个函数,该函数使用模板创建类或子类的对象
- 我想要一个具有子函数的函数访问相同的命名函数,而不使用它取决于其子类的类
- 我们可以在没有新实例化的情况下声明一个抽象方法来返回抽象超类中的子类对象吗
- 是否可以使一个类成为两个不同层次结构的子类?
- 如何将抽象类的可访问性限制为另一个抽象类及其子类?
- 是否可以为所有子类设置一个实例?
- 模板子类的一个示例
- 我们如何有证据表明,声明虚拟函数的类是2个字节,其中一个不超过一个未声明,而在子类中
- QT子类UI表单显示一个空白窗口
- C++:从两个包含子类 (typedef) 的列表创建一个列表
- 我有一个为 cpp 中的某个类定义的模板函数 - 不是 hpp.可以由子类使用吗?
- 如何创建一个包含C 中的子类的类的链接列表
- 如何处理一个子类有方法,而另一个没有方法的子类?
- 如何从另一个类访问一个类的方法/变量,而不在c++中实例化它或子类
- 将QQuickItem指针的子类指定给另一个C++对象
- C++在子类中创建一个可用的方法
- 为什么Qt提供了一个类来对其进行子类化