I'm working on a game engine and working on implementing a state design. I have an Engine class which takes care of all the initialization of everything and contains the game loop which calls update, render and handle input functions of the active state.
All my different states inherit from State which requires a reference to the Engine class in its constructor, in order to initialize the protected reference of the engine for future use. Here's the relevant code:
// file: state.h
class Engine;
class State {
public:
State(Engine &engine) : mEngine(engine) { }
protected:
Engine &mEngine;
};
// file: gamestate.h
class GameState : public State {
public:
GameState(Engine &engine) : State(engine) {}
};
and finally in engine.cpp in the initializer I create a new GameState object, which is where the error is reported.
GameState *state = new GameState(&this);
I'm coding it in C++ using Qt Creator on Linux at the minute, don't have access to a windows machine right now to see if it's a problem with the gcc or not.
Change:
GameState *state = new GameState(&this);
to:
GameState *state = new GameState(*this);
This is because you are passing the Engine by reference to the constructor of the State class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With