Archived

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

Draco5869

Passing Iterators into Functions

Recommended Posts

Draco5869    122
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
Draco5869    122
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
Draco5869    122
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
Draco5869    122
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
Draco5869    122
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