-3

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();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Devkg
  • 3
  • 1
  • 2
  • 1
    Yes, because `GameState` does not implement several abstract functions you cannot construct a new instance of a `GameState` object, using `new`, or in any other way; so what exactly about this basic property of abstract, a.k.a. pure virtual, methods, you are asking about? Just by eyeballing the shown code, `GameState` fails to implement `Update()`, and `Render()`, so either implement them, or create some other object that implements them. That's it. – Sam Varshavchik Mar 05 '19 at 02:17
  • 3
    You should annotate all your overriders with `override`, then you will very quickly spot the problem. – Kerrek SB Mar 05 '19 at 02:23
  • Don't forget about good ol' function overloading in C++: `void Update()` and `void Update(Level&, Player&, int&)` are **different functions**. You would benefit from adding the [`override`](https://stackoverflow.com/questions/18198314/what-is-the-override-keyword-in-c-used-for) specifier to your functions in `GameState`. – alter_igel Mar 05 '19 at 02:32
  • 1
    Possible duplicate of [Error message: Object of abstract class type "X" is not allowed: Pure virtual "Y" has no overrider](https://stackoverflow.com/questions/50053589/error-message-object-of-abstract-class-type-x-is-not-allowed-pure-virtual-y) – Raymond Chen Mar 05 '19 at 14:08

1 Answers1

0

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.

Eli
  • 693
  • 5
  • 12