I'm trying to create an abstract class, which is used as a "template" for deriving classes. I created a vector (stack) in which I store pointers to said abstract class, since you can't call member functions from an abstract class itself (thankfully ;))
But when I compile my classes, I get the following error:
error: invalid use of incomplete type 'class GameState'
this->_states.top()->Cleanup();
^~
I already tried many things and I think the problem is, that I have a forward declaration of the GameState class inside the Game class, because they both have to know of each other. Could that be the problem?
For viewing purposes, here are the snippets of my code:
GameState.hpp:
#include "Game.hpp"
class GameState {
public:
Game *game;
virtual void Init()=0;
virtual void Cleanup()=0;
... (etc.)
protected:
GameState(){}
};
Game.hpp:
#include <stack>
#include <SFML/Graphics.hpp>
#include "TextureManager.hpp"
// I GUESS THAT THIS DECLARATION IS THE PROBLEM!
class GameState;
class Game {
public:
TextureManager *TextureMgr;
sf::RenderWindow Win;
Game();
~Game();
... (etc.)
bool IsRunning() { return this->running; }
bool Quit() { this->running = false; }
private:
std::stack<GameState*> _states;
bool running;
};
Game.cpp:(calling example of member function)
void Game::Cleanup() {
while (!this->_states.empty()) {
this->_states.top()->Cleanup();
this->_states.pop();
}
}
Thank you very much in advance. And if the forward declaration is the problem; do you probably know any article or book, where such problems, where two classes have to know of each other, are described with different approaches for a solution? I've already searched in a bunch of books, but I can't really find one, which got my eye.
You need to include GameState.hpp
into Game.cpp
so GameState
will be complete at that point. Forward declaration allows to declare std::stack<GameState*>
however a definition should be available to call class methods.