Archived

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

Passing Iterators into Functions

This topic is 5273 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

Alrighty, writing tetris game. When i want to delete a whole line, it''s done in the Tetris.cpp file, but the sprite and gameengine are their own classes and the sprite vector is in the gameengine. So, i write a gameengine function to erase a certain sprite. Now, i want to pass in the ITERATOR to the sprite, delete that sprite from memory and then erase it from the vector.
if ((*siSprite)->GetYPosition() == y)//if it''s on that line

		{
			delete (*siSprite);//delete it

			_pGame->GetSpriteVector().erase(siSprite);//get it outta vector

			siSprite--;//move the movement backwards to account

		}
But, i get an access violation if i don''t do it in a gameengine function and i don''t exactgly know about passing in iterators and how that would work. Help por favor ----------------------------------------------------------- Like a sponge!

Share this post


Link to post
Share on other sites
Actually, i think the scope prob could be in the "GetYPosition()" of the sprite... Here''s the whole function.


void DeleteLine(int y)
{

vector<Sprite*>::iterator siSprite;
for (siSprite = _pGame->GetSpriteVector().begin();
siSprite != _pGame->GetSpriteVector().end(); siSprite++)
{
if ((*siSprite)->GetYPosition() == y)//if it''s on that line

{
delete (*siSprite);//delete it

_pGame->GetSpriteVector().erase(siSprite);//get it outta vector

siSprite--;//move the movement backwards to account

}
}


-----------------------------------------------------------
Like a sponge!

Share this post


Link to post
Share on other sites
Alright, made it into a gameengine function, but i still get an access error on the GetYPosition()???

^^Unhandled exception at 0x00425cc5 in Tetris.exe: 0xC0000005: Access violation reading location 0x00a64000.

[edited by - draco5869 on July 7, 2003 12:24:46 AM]

Share this post


Link to post
Share on other sites
But somehow the function from the book works that''s basically the same thing:: okay, i''ll let someone else post now =)


void GameEngine::CleanupSprites()
{
// Delete and remove the sprites in the sprite vector

vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
delete (*siSprite);
m_vSprites.erase(siSprite);
siSprite--;
}
}

Share this post


Link to post
Share on other sites
Now it''s like it''ll delete the line, leave the others there, and still error after it''s done?

-----------------------------------------------------------
Like a sponge!

Share this post


Link to post
Share on other sites