Jump to content

Error "std::length"


Krankzinnig
 Share

Recommended Posts

I have a vector which is my stack of game states. When I create the first scene, which is just calling ChangeState, I get the std::length error. I understand what this means but I cannot fix it, I don't know why its happening in the first place. This code is working fine in my other project. But when I tried to port it into a static library I got this error when using the library. Here is where the error gets thrown:

 

void Core::ChangeState(StateManager *State) 
{
if(States.empty() != NULL)
{
	States.back()->Destroy();
	States.pop_back();
}

// Store and initialize the new state
States.push_back(State); // Throws std::length error
States.back()->Initialize();
}

 

Here is where I declare the vector.

 

namespace Elixir
{
class StateManager;

class Core
{
	public:
		void Initialize();
		bool Running();
		void Destroy();

		void ChangeState(StateManager* State);
		void PushState(StateManager* State);
		void PopState(StateManager* State);

		void Events();
		void Update();
		void Draw();

		bool Quit;

	private:
		// This is the stack of game states
		vector<StateManager*> States;
};
}

 

Any help would be appreciated. I have reached out to a few people for help on this issue and they all seemed like they didn't know and stopped helping.

AMD Phenom II X4 B55 3.20 GHz - 8.00 GB Patriot Gamer Series RAM - AMD Radeon HD 6800 Series 1 GB GDDR5 - Windows 7 Professional 64 Bit

Link to comment
Share on other sites

Are you sure it's failing on the push_back statement? If I put that code in it fails on States.back()->Destroy(); because I don't think the NULL check you have it the right way to check if the vector has items or not.

 

Do something like if(!States.empty()) instead. Once I do that, this all works for me. I assume you are stepping through your code to see what values are and such?

Link to comment
Share on other sites

Interesting you mentioned using if(!States.empty()) because that's what I had in the first place. But it kept going into that statement. I guess the real problem is that its not empty in the first place. But still don't know how to fix that, because it should be. Yeah, I have stepped through 100+ times. Its a tool I use multiple times a day. Lately its been on the hour.

AMD Phenom II X4 B55 3.20 GHz - 8.00 GB Patriot Gamer Series RAM - AMD Radeon HD 6800 Series 1 GB GDDR5 - Windows 7 Professional 64 Bit

Link to comment
Share on other sites

What compiler are you using? I did this with VS 2005 pro and it worked perfectly once I changed to !empty(). Some implementations of the stl might be slightly different maybe?

 

I assume your entire code is huge? Otherwise post it and we can try it. Might be something going on somewhere else.

Link to comment
Share on other sites

Yeah, the most confusing part is that the same source code works beautifully in my other project. I wanted to port this feature along with many others to a library I can use for my two game titles. Its at this point, when I created a static library that the statemanager stopped working with odd vector issues. So, I scrapped it all out and cleaned my library to try to implement JUST THIS feature. I will let you know soon when I get this done, I can then post more code.

AMD Phenom II X4 B55 3.20 GHz - 8.00 GB Patriot Gamer Series RAM - AMD Radeon HD 6800 Series 1 GB GDDR5 - Windows 7 Professional 64 Bit

Link to comment
Share on other sites

There are some things that will not work as expected:

if(States.empty() != NULL)

Wait whut? If bool != 0? Which warning level are you compiling at. This should warn for conversion. Let's say .empty() returns true, you will do if(true != false), else if(false != false)... The check should be:

if(States.empty() == false)

 

Another thing would be the back() and pop_back() calls. pop_back returns the element, so you can just pop_back()->Destroy() it. Isn't this leaking? State is a pointer, what does Destroy do?

 

What IDE are you using? A possible problem could be if you're using prior to VS2010, that you build the lib in SCL and the test-project (or whatever you name it) in HDI (debug vs release). What actually happens here is that the sizes of vector aren't the same, so hence an error.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...