(我很新的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;
};
''玩家'必須覆蓋它所派生類的所有純虛函數**。 –