Help with iterators.

Hi. Im having a problem with a function in my class that uses vectors and iterators. I know it is this function as when I comment it out it works fine. I am trying to set the x and y positions back to the start when the objects in question move off the screen. Btw, the error is a run time error. "vector iterator not incrementable". Thanks in advance. Here is my code.

void EnemyManager::initialize()
{
for(iter = enemiesVec.begin(); iter < enemiesVec.end(); iter++)
{
(*iter)->setIsActive(true);
if((*iter)->getIsActive() == true)
{
(*iter)->setSpriteX(x);
(*iter)->setSpriteY(y);
}
}
}
1. Use code tags!
2. What is the declaration of the iter variable? And why do you have the need to declare it outside the initialize() method?
(*iter)->setIsActive(true);
Is your vector a vector of pointers?
an iterator isn't an integer and you can't treat it like an integer. So this, " iter++ " is impossible.

may i suggest this instead?

1
2
3
4
5
6
7
8
9
10
11
12
void EnemyManager::initialize()
{
      for (int i = 0; i < (int) enemiesVec.size(); i++)
     {
           enemiesVec[i]->setIsActive(true);
           if(enemiesVec[i]->getIsActive() == true)
           {
                 enemiesVec[i]->setSpriteX(x);
                 enemiesVec[i]->setSpriteY(y);
           }
      }
}


EDIT: actually, what i said was entirely incorrect, and what you did should work right, just went over the documentation again :} fail. Back to what webJose asked...
Last edited on
1. Do any of the methods you call delete elements or modify the vector content somehow? http://stackoverflow.com/questions/3779227/why-is-this-vector-iterator-not-incrementable
2. Pls pre-increment iterators :)
iter++ " is impossible.
This statement is wrong. You can do iter++ for any type of iterator
yerp, corrected myself already :P
Iter is declared in the .h for the class. And also the vector is a vector of pointers. Ill try a couple of these suggestions thanks.
Thanks for everyones help. I fixed it. It tried a public declaration of the iterator and it works. Strange really because im still using "iter" elsewhere in the class, only in that 1 function do I need to change how it's done. Here is my amended code.

void EnemyManager::initialize()
{
vector<Enemy*>::iterator it;
for(it = enemiesVec.begin(); it < enemiesVec.end(); it++)
{
(*it)->setIsActive(true);
if((*it)->getIsActive() == true)
{
(*it)->setSpriteX(x);
(*it)->setSpriteY(y);
}
}
}
And yet you didn't use code tags again.

Can you post the declaration of the class?
Last edited on
Code tags!!! It isn't hard at all:

[code]
1
2
vector<Enemy*>::iterator it;
for(it = enemiesVec.begin(); it < enemiesVec.end(); it++)
[/code]

See??? Simple.
Why dont you directly set the x and y value

1
2
3
4
5
6
7
8
9
void EnemyManager::initialize()
{
		for(iter = enemiesVec.begin(); iter < enemiesVec.end(); iter++)
		{
					(*iter)->setSpriteX(x);
					(*iter)->setSpriteY(y);
			
		}
}


Wont this work for you
1
2
3
4
5
6
7
8
9
10
11
12
13
void EnemyManager::initialize()
{
	vector<Enemy*>::iterator it;
	for(it = enemiesVec.begin(); it < enemiesVec.end(); it++)
	{
		(*it)->setIsActive(true);
		if((*it)->getIsActive() == true)
		{
			(*it)->setSpriteX(x);
			(*it)->setSpriteY(y);
		}
	}
}
Sorry. Didnt know how to use code tags before. Anyway here is my code again. Thanks.
When incrementing an iterator use the prefix ++ instead of the postfix ++. The postfix ++ creates a temporary variable before incrementing the iterator, while the prefix just increments the iterator. The code for the for statement should look like the following:

for(it = enemiesVec.begin(); it < enemiesVec.end(); ++it)
Topic archived. No new replies allowed.