Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

corrington_j

problem with stl stack

This topic is 5230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the stl stack for my games state manager and my game keeps crashing. Here is the code for my state manager, and then I will further explain the problem.

#include <stack>
using namespace std;

class Cstate
{
public:
	bool SetDevices(CinputDevice *kb, Cgraphics *gfx);

	virtual bool Init() {return true;}
	virtual bool Shutdown() {return true;}
	virtual bool Frame() {return true;}

protected:
	CinputDevice *keyboard;
	//mouse

	//gamepads


	Cgraphics *graphics;
};


class CstateManager
{
public:
	bool Push(Cstate *state);
	bool Pop();

	bool Init();
	bool Shutdown();
	bool Frame();

	int GetNStates();
	Cstate *GetTop();

private:
	stack<Cstate *> stateStack;
};

bool CstateManager::Frame()
{
	return stateStack.top()->Frame();
}

//set up states

CstateManager stateManager; //state manager

//states

CmenuState mainMenuState;

mainMenuState.SetDevices(&keyboard, &graphics);
		
//set up the state manager

stateManager.Push(&mainMenuState);

CmenuState is derived from Cstate It crashes when I call stateManager.Frame(). And it says unhandled exception... access violation reading location 0x00000000. It breaks on this line, return stateStack.top()->Frame();. Does anyone know what the problem is. Also this is my first attempt at a state manager, and im not really sure if i did a very good job on it, does anyone have some suggestions for it.

Share this post


Link to post
Share on other sites
Advertisement
When do you call CstateManager::Frame()? Is it possible that Frame() is being called before you push anything on the stack?

Share this post


Link to post
Share on other sites
Ok i checked it out, and the state is not NULL before it gets pushed onto the stack, but once on the stack it is showing that stack.top() = NULL. what is going on.

Share this post


Link to post
Share on other sites
What does the CstateManager::Push() function look like?

edit: !@#$! smiley code

[edited by - SiCrane on February 27, 2004 11:56:30 PM]

Share this post


Link to post
Share on other sites
if (state = NULL)

should be

if (state == NULL)

You''re assigning NULL to state instead of checking state against NULL. Probably a sign that you''re compiling with the warning level too low.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!