0
votes

Can someone point me in the right direction of where to look please? I'm getting the above error.

std::vector of class States declared in game engine class.

class GameEngine
{....
 private:
 std::vector<State> *m_states;
}

Within the Constructor:

m_states = new std::vector<State>;

Add state function

void GameEngine::AddState(State *state, bool change)
{
   m_states.push_back (state);
   .....
}

"Error 3 error C2228: left of '.push_back' must have class/struct/union"

Thanks in advance.

2
Dont use new std::vector<State> *m_states; -> std::vector<State> m_states; (your original error is because you forgot to dereference : m_states->push_back (state); ) - quantdev

2 Answers

1
votes

You have declared m_states as a pointer ...

...
private:
 std::vector<State> *m_states;
...

so, you have to use it as a pointer.

Write m_states->push_back instead of m_states.push_back.

On the other hand I can see you have another error in your code:

your GameEngine::AddState(State *state, bool change) function receives a State * as the first argument, but your m_states member is a vector of State objects not pointers. You have to change your declaration to:

std::vector<States *> *m_states;

Advice: Try to avoid using pointers (*) in C++, use smart_pointers instead.

0
votes

Since you are creating a pointer, you need to dereference it before pushing back anything into the vector.

(*m_states).push_back (state);

Or simply:

m_states->push_back (state);