Reputation: 37
So for my next game I am creating classes to better the architecture of my game I have a abstract class called GameState and all GameStates (menus, windows, screen, the game itself) must inherit it to be added to the game but my BlankState which is a test of my GameState design gets me in trouble with the compiler as it is, for some reason, considered an abstract class, can anyone tell me why? I feel like I am missing something really obvious here.
Here is the GameState.h contents
#ifndef GUARD_GAMESTATE_H
#define GUARD_GAMESTATE_H
#include<SDL.h>
class Game;
class GameState {
protected:
Game *m_pGame;
public:
GameState( Game& );
virtual void load() = 0;
virtual void handle_events( SDL_Event& events ) = 0;
virtual void logic() = 0;
virtual void draw() = 0;
virtual void unload() = 0;
virtual ~GameState() = 0;
};
class BlankState : public GameState {
private:
SDL_Surface *background;
public:
BlankState(Game& game);
void load();
void handle_events();
void logic();
void draw();
void unload();
~BlankState();
};
#endif
And here is the GameState.cpp where I implement the GameState constructor and BlankStates inherited methods
#include"GameState.h"
#include"Game.h"
#include"AssetLoader.h"
GameState::GameState(Game& game) {
m_pGame = &game;
m_pGame->addState(this);
}
BlankState::BlankState(Game& game)
:GameState(game)
{}
void BlankState::load() {
background = AssetLoader::loadImage("assets\background.png");
}
void BlankState::handle_events() {}
void BlankState::logic() {}
void BlankState::draw() {
SDL_Rect lol;
lol.x = 0;
lol.y = 0;
SDL_BlitSurface(background, NULL, m_pGame->getSurface(), &lol);
}
void BlankState::unload() {
SDL_FreeSurface(background);
}
BlankState::~BlankState() {}
Thanks in advance for your help
Upvotes: 1
Views: 83
Reputation: 126432
The void handle_events()
function in the derived class has a different signature than the void handle_events(SDL_Event&)
function in the base class it is supposed to override.
The compiler is complaining because the derived class does not provide an implementation of the pure virtual function void handle_events(SDL_Event&)
declared in the base class.
In other words, your derived class is abstract and the compiler correctly refuses to instantiate it.
As an additional remark, in C++11 you can use the contextual keyword override
to explicitly state your intention of making a member function override a virtual function declared in the base class:
struct X
{
virtual void foo(int) = 0;
};
struct Y : X
{
virtual void foo(int) override { }
// ^^^^^^^^
// Makes it clear that this function is meant to
// override (and not just to hide) a virtual function
// declared in the base class
};
int main()
{
Y y;
}
The purpose of this is that errors like the one you made could have been easily caught. Trying to override
a function whose signature is not compatible with the signature of a virtual function in the base class would trigger a compilation error:
struct X
{
virtual void foo(int) = 0;
};
struct Y : X
{
virtual void foo() override { } // ERROR!
// ^^^^^^^^
// X::foo has an incompatible signature
};
int main()
{
Y y;
}
Upvotes: 5