Public Group

Archived

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

What's wrong with my vector?

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

Recommended Posts

Hey, I have a problem with vector.. everything seems fine to me and I really don't understand what can cause the problem. My vector work correctly, but the problem occurs when I erase something from my vector... If I want to remove the second element, I will do this: int num = 1; m_Vector.erase(m_Vector.begin()+num); This works fine.. but when I erase something, immediatly after, I will enter this vector loop: for(int i=0; i Someone know what could be the cause of the problem? Thanks [edited by - Hedos on March 7, 2004 1:02:43 AM]

Share on other sites
quote:

m_Vector.erase(m_VectorGrass.begin()+num);

You''re erasing something from a different vector than the vector you got the iterator from?

Share on other sites
I invite you to post your actual code section from where you created the vector to where the crash occurs.

Share on other sites
quote:

You're erasing something from a different vector than the vector you got the iterator from?

Sorry, that was an mistake in my post, I edited it

Ok, so here is my actual code:
cGrass	m_GrassDefault;std::vector<cGrass> m_VectorGrass;m_VectorGrass.reserve(1024);m_VectorGrass.clear();bool cWorld::GrassCreate(int tileNum){	m_GrassDefault.SetTileNum(tileNum);	m_GrassDefault.RandomizeDelay();	m_GrassDefault.RandomTile();	m_GrassDefault.SetLastDelay(timeGetTime());	m_VectorGrass.push_back(m_GrassDefault);	return true;}void cWorld::GrassRemove(int num){	m_VectorGrass.erase(m_VectorGrass.begin()+num);}void cWorld::GrassDoProcess(){  for(int i=0; i<m_VectorGrass.size(); i++)  {    m_VectorGrass[i].DoProcess(); //Crash right here if I erased something  }}//Then I have my game loop//GrassDoProcess() is called every frame//I can add elements with mouse click//I use m_World.GrassCreate(tileNum); to add an element//Everything works fine, except if I try to remove an elementif(bRButton){  m_World.GrassRemove( someElement );}//After removing something, the game will crash in the GrassDoProcess loop

[edited by - Hedos on March 7, 2004 1:07:41 AM]

[edited by - Hedos on March 7, 2004 1:12:05 AM]

Share on other sites
hmm.... also show the code where you added an element to the vector and stored its iterator in someElement. (oh, and you should probably use [ source ] tags rather than [ code ] tags; they''re nicer looking and preserve indentation.)

"Sneftel is correct, if rather vulgar." --Flarelocke

Share on other sites
Well, think about it. When iterating through the vector, you grab an iterator for an element which is currently in the vector. Then, while you''re iterating, you remove the element. This means that the iterator is no longer part of the vector, so you can''t go to the next element in the vector. In short, you have to hold the value of the previous element, delete the current element, then increment the previous iterator.

Share on other sites
quote:
Original post by Sneftel
hmm.... also show the code where you added an element to the vector and stored its iterator in someElement. (oh, and you should probably use [ source ] tags rather than [ code ] tags; they''re nicer looking and preserve indentation.)

"Sneftel is correct, if rather vulgar." --Flarelocke

I added the code where I add elements.. but what do you mean by iterator?
I''ve heard a little bit about iterators but I really don''t know what it is used for?

Share on other sites
quote:
Original post by psykr
Well, think about it. When iterating through the vector, you grab an iterator for an element which is currently in the vector. Then, while you''re iterating, you remove the element. This means that the iterator is no longer part of the vector, so you can''t go to the next element in the vector. In short, you have to hold the value of the previous element, delete the current element, then increment the previous iterator.

wrong. All that has to be done is assign the return from calling the erase() member back to the iterator that will set it in the next position following the deleted element.

Share on other sites
quote:
Original post by Hedos

yes, its a good idea in general, however, I can''t seem to pick up on whats happening in your code. Specifically what is DoProcess() doing? What are the compiler errors saying? Perhaps others here might be seeing something with what code you posted that I having noticed.

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5

• 12
• 16
• 13
• 20
• 12
• Forum Statistics

• Total Topics
632176
• Total Posts
3004588

×