错误 1 错误 C2259:'Player':无法实例化抽象类
Error 1 error C2259: 'Player' : cannot instantiate abstract class
为什么我不能实例化?为什么播放器是一个抽象类?我是否只在基类中声明了纯虚拟函数?
class Object
{
protected:
public:
virtual void DrawOnScreen() = 0;
};
class Creature : public Object
{
public:
virtual void DrawOnScreen()=0;
virtual void Eat(Object*)=0;
virtual void move(Direction)=0;
};
class Player : public Creature
{
void Player::DrawOnScreen()
{
cout << "position= (" << get_x() << "," << get_y() << ")" << endl << "player's score " << points << endl;
if (get_isKilled()) cout << "It is Killed " << endl;
else cout << "It is Alive " << endl;
}
void Player::eat(Object* p)
{
Point* chk;
chk = dynamic_cast<Point*>(p);
if ((chk != 0) && (get_x() == chk->get_x()) && (get_y() == chk->get_y()) && (chk - > get_isExist()))
{
points = points + chk->get_weight();
chk->set_isExist(false);
}
}
void Player::move(Direction d)
{
if (d == UP)
{
y = y + 2;
}
if (d == DOWN)
{
y = y - 2;
}
if (d == RIGHT)
{
x = x + 2;
}
if (d == LEFT)
{
x = x - 2;
}
}
}
问题是,由于继承和错误的重写,您无法使用至少一个纯虚拟方法实例化类,而这种情况在这里发生。
这是一个拼写错误:
void Player::eat(Object*p)
你真正的意思是用资本"吃饭",因此你实际上并不是凌驾于一切之上。你应该写这样的:
void Player::Eat(Object*p)
除此之外,您确实应该删除类中方法的Player::
作用域,或者将类似的方法移到外部。
此外,请充分利用C++11 override关键字来避免此类问题,因此类似这样的事情会给您带来编译错误:
void Eat(Object *p) override;
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误的cv::face FacemarkLBF实例化
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- gcc出现不明确的模板实例化错误
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 无法实例化抽象类错误
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- 模板实例化分析错误
- unique_ptr 使用尚未定义的参数进行实例化不会导致错误
- 与模板实例化相关的编译错误
- 从模板实例化/类型推断中查找错误消息的实际来源
- 类模板实例化错误
- 错误 C2908:显式专用化; 已被实例化
- C++11 列出 push_back() 实例化错误
- 如何修复错误C2259:无法实例化抽象类
- 使用非量表类型实例化模板时修复编译错误
- 由于命名空间而导致的对象实例化错误
- 无法在好友函数中实例化类?我没有得到在范围错误中声明
- 实例化对象时如何修复分段故障错误
- 将 SFINAE 上下文中不正确的模板实例化的硬错误转变为软错误