Expression, Vector subscript out of range

Hey guys!

I'm getting this error whenever I try and compile my code. It's for a school assignment and my friend has essentially the same code and his compiles with no issues at all, could anyone shed some light on why this is happening? Cheers!

Obviously if more code is needed then it shall be provided!

Debug Assertation Failed!

Expression: vector subscript out of range.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class StateContext
{
protected:
	State* currentState;
	vector<State*> availableStates;
	vector<int> stateParameters;

public: 
	~StateContext()
	{
		for (int i = 0; i < availableStates.size(); i++)
		{
			delete availableStates[i];
		}
	}
	void setState(state newState)
	{
		this->currentState = availableStates[newState];
	}
	void setStateParam(stateParameter SP, int value)
	{
		this->stateParameters[SP] = value;
	}
	int getStateParam(stateParameter SP)
	{
		return this->stateParameters[SP];
	}
};
Last edited on
read below
Last edited on
The code is obviously not the whole code but the error message is clear enough to say what the problem is. "vector subscript out of range" means that you are trying to access an element in the vector that doesn't exist. You could get that error if you try to access stateParameters[SP] when SP >= stateParameters.size().
And 'Debug Assertation Failed' is a runtime error not at compile time.
Topic archived. No new replies allowed.