Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ lvalue required as unary '&' operand

Tags:

c++

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.

like image 876
grouse Avatar asked Feb 24 '23 10:02

grouse


1 Answers

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.

like image 186
user258808 Avatar answered Feb 25 '23 22:02

user258808