有人可以帮我吗?
我现在有这个问题,无法解决。
不允许使用抽象类类型的对象:纯虚函数没有覆盖程序”
我在下面突出显示了
class State
{
public:
virtual void Update() = 0;
virtual void Render();
virtual void Enter() = 0;
virtual void Exit() = 0;
virtual void Resume() = 0;
protected:
State() {}
};
class GameState : public State
{
private:
Level level;
Level levels[5] = { Level(3), Level(1), Level(1), Level(2), Level(1) };
const char* levelNames[5] = { "Level1.txt", "Level2.txt", "Level3.txt", "Level4.txt", "Level5.txt" };
SDL_Texture* tileText;
SDL_Texture* playerText;
Player player;
SDL_Surface* tileSurf;
SDL_Surface* playerSurf;
int m_iTickCtr = 0;
int m_iTickMax = 1; // for animation
public:
GameState() {}
void Update(Level& l, Player& p, int& c) ; // Level& level, Player& player, int& c
void Render(Level& l, Player& p);
void Enter();
void Exit();
void Resume() { cout << "Resuming Game..." << endl; }
enum sfx { jump, boom, laser };
};
class TitleState : public State
{
public:
TitleState() {}
virtual void Update();
void Render();
void Enter();
void Exit();
void Resume() {}
enum btn { play, exit };
private:
vector<Button*> m_vButtons;
};
void TitleState::Update()
{
/*
if (Game::Instance()->KeyDown(SDL_SCANCODE_B) == 1)
Game::Instance()->GetAM()->PlaySound(sfx::boop);
*/
for (int i = 0; i < (int)m_vButtons.size(); i++)
m_vButtons[i]->Update();
// Parse buttons.
if (m_vButtons[btn::play]->Clicked())
Game::Instance()->GetFSM()->ChangeState(new GameState()); // here 'GameState' has problems.
else if (m_vButtons[btn::exit]->Clicked())
Game::Instance()->DoQuit();
}
答案 0 :(得分:0)
在子类GameState
中,必须为void Update()
创建覆盖函数,该函数在抽象类State
中定义为纯虚函数。
也许您认为是这样做的,因为您创建了void Update(Level& l, Player& p, int& c)
,但是由于此函数的签名与void Update()
不同,因此不会覆盖它。