Help with iterators.

Nov 29, 2011 at 3:03pm
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);
}
}
}
Nov 29, 2011 at 3:11pm
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?
Nov 29, 2011 at 4:09pm
(*iter)->setIsActive(true);
Is your vector a vector of pointers?
Nov 29, 2011 at 4:52pm
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 Nov 29, 2011 at 4:59pm
Nov 29, 2011 at 4:53pm
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 :)
Nov 29, 2011 at 4:58pm
iter++ " is impossible.
This statement is wrong. You can do iter++ for any type of iterator
Nov 29, 2011 at 5:22pm
yerp, corrected myself already :P
Nov 29, 2011 at 5:31pm
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.
Nov 29, 2011 at 5:37pm
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);
}
}
}
Nov 29, 2011 at 5:39pm
And yet you didn't use code tags again.

Can you post the declaration of the class?
Last edited on Nov 29, 2011 at 5:41pm
Nov 29, 2011 at 5:42pm
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.
Nov 29, 2011 at 5:44pm
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
Nov 29, 2011 at 6:09pm
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);
		}
	}
}
Nov 29, 2011 at 6:12pm
Sorry. Didnt know how to use code tags before. Anyway here is my code again. Thanks.
Nov 29, 2011 at 6:31pm
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.