(我很新的C++,所以希望这只是一个新手的错误)C++ - 无法实例化抽象类
我有我的代码,在那里我有一个类“玩家”的问题,需要一些属性,其中我尝试给它虽然使用抽象类的这样:
//player.h
class Player : public IUpdate, public IPositionable, public IMoveable, public IDrawable
{
public:
Player(void);
SDL_Rect get_position();
void move(Uint32 dTime);
void update(Uint32 dTime);
void show(SDL_Surface* destination);
~Player(void);
private:
SDL_Surface texture;
int x, y;
};
而且我重写纯虚函数这样:
//Player.cpp
Player::Player(void)
{
}
SDL_Rect Player::get_position()
{
SDL_Rect rect;
rect.h = 0;
return rect;
}
void Player::move(Uint32 dTime)
{
}
void Player::update(Uint32 dTime)
{
move(dTime);
}
void Player::show(SDL_Surface* destination)
{
apply_surface(x, y, &texture, destination, NULL);
}
Player::~Player(void)
{
}
然而我不断收到合作mpilation错误:C2259: 'Player' : cannot instantiate abstract class
据我所见,纯粹的虚拟功能应该被覆盖,我的谷歌搜索告诉我,会使得Player非抽象,但Player仍然看起来很抽象。
编辑: 纯虚函数:
class IPositionable
{
public:
virtual SDL_Rect get_position() = 0;
private:
int posX, posY;
};
class IUpdate
{
public:
virtual void update (Uint32 dTime) = 0;
};
class IMoveable
{
public:
int velX, velY;
virtual void move(Uint32 dTime) = 0;
};
class IDrawable
{
public:
virtual void show() = 0;
private:
SDL_Surface texture;
};
class IHitbox
{
virtual void check_collsion() = 0;
};
class IAnimated
{
virtual void next_frame() = 0;
int state, frame;
int rows, columns;
};
''玩家'必须覆盖它所派生类的所有纯虚函数**。 –