Variable gets set to random number?

The function is suppose to take the currentState and nextState, delete currentState, set currentState to a new thing depending on what nextState was, change stateID(global) to nextState, and nextState to NULL.

The problem occurs on the bolded line. When currentState is deleted, nextState is set to -17891602. Since it is now set to that strange number, it cant function properly in the switch statement. Please help.

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
GameStates ChangeState( GameState *currentState, GameStates &nextState )
{
	//If the state needs to be changed
	if( nextState != STATE_NULL )
	{
		//Delete the current state
		if( nextState != STATE_EXIT )
		{
			delete currentState;
		}

		//Change the state
		switch( nextState )
		{
		 case STATE_TITLE:
			currentState = new Title();
			break;
		  case STATE_2PLAYER:
			currentState = new MultiPlayer();
			break; 
		}
		stateID = nextState;

		nextState = STATE_NULL;
	}
	return stateID;
}
Are you sure currentState points to a valid object? You need to at least check if for NULL!
Well, the case that nextState is none of the three states mentioned isn't handled, which would result in a double delete. But more importantly, you're assigning the new states to the local pointer variable currentState which is lost when the function exits.
Dealing with raw pointers (and manual deletes) is usually a bad idea. Use std::unique_ptr (or shared_ptr if necessary) to avoid such mistakes in future.

Edit:
You need to at least check if for NULL!

You don't. Deleting a null pointer is okay.
Last edited on
I thought if i passed it as a pointer then what ever changes in the function changes everything?
I got it to work if i do this:
1
2
3
4
5
6
7
8
9
10
11
switch( nextState )
		{
		 case STATE_TITLE:
			currentState = new Title();
			break;
		  case STATE_2PLAYER:
			currentState = new MultiPlayer();
			break; 
                  default:
                        currentState = new Title();
		}




But that doesnt really fix the problem once i have to change to a state other than title. Does anyone know whats going wrong with nextState?
I assume by "everything", you mean the original object.
That would be true if you were changing the object currentState points to (as in *currentState=...), but you're not. You're just assigning a new value to the pointer itself (more precisely, to a copy of the pointer, since you're passing it by value).
Last edited on
Im sorry but i cant figure but how to do that athar. I tried doing
*currentState = newIntro();

but it says "no operator '=' matches these operands".

EDIT: Wait a minute the original currentState must be being changed otherwise it wouldnt work at all. You probably missed my previous post but i got it to work if i put that default into the swtich statement. I dont think the problem is with currentState, but with nextState.
Last edited on
Idk if double posting is against the rules, but i think figured out the problem.

nextState is a member variable of currentState. So when currentState is deleted so is nextState?

So then nextState cant do its work in the switch statement. How would i fix this?

here is some background information:

- currentState is an object of different states. each state is derived from a GameState class
- the GameState class has a member variable nextState( so currentState also has that variable )
- ChangeState is a function in main.cpp
but it says "no operator '=' matches these operands".

Well, new returns an address, so you cannot assign that to a GameState object.
What you need is a reference to a pointer (or rather, a reference to a unique_ptr).

Wait a minute the original currentState must be being changed otherwise it wouldnt work at all.

The original pointer is not being changed. A possible reason it might seem as if it worked is that new might happen to create the new object at the location of the one you just deleted before.

How would i fix this?

By passing nextState by value and setting the new state on the new object.
Last edited on
By passing nextState by value and by setting the new state on the new object.

Im not really sure what you mean by that but if i try doing *currentState = new Title() it says "no operator '=' matches these operands."

EDIT:

Okay i just need a way to make currentState change to a new Title() everywhere. I dont really understand pointers and references so could you just make it as simple as possible? maybe even posting some code?
Last edited on
Topic archived. No new replies allowed.