grouse
grouse

Reputation: 43

C++ lvalue required as unary '&' operand

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.

Upvotes: 4

Views: 28262

Answers (1)

user258808
user258808

Reputation: 734

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.

Upvotes: 8

Related Questions