Search code examples
c++virtual

pure virtual function has no overrider


Can anyone help me please?

I have this problem now, and cannot solve this.

object of abstract class type is not allowed: pure virtual function has no overrider'

I highlighted below:

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();
}

Solution

  • In your subclass, GameState, you must create an override function for void Update(), which is defined as pure virtual in the abstract class, State.

    Maybe you think you did so, because you created void Update(Level& l, Player& p, int& c), but since this function has a different signature than void Update(), it does not override it.