1

Here is the code I have that whenever I run throws a "Iterator not incrementable" error at run time. If I comment out the sp++ line or the asteroids.push_back(*sp); line then it runs fine. So it has something to do with those lines...I saw in a previous post that the line sp->getSize() is incremented the pointer as well and may be the cause of the issues? thanks for the help!

    while(sp != asteroids.end()){ 
                if(sp->getSize() == .5 || sp->getSize() == 0.25){
                    glPushMatrix();
                    glScalef(.1, .1, .1);
                    glTranslatef(3,3,0);
                    sp->display_asteriod(sp->getSize(), random, randomTwo);
                    glPopMatrix();

                    asteroidCount++;
                    spawn.setSize(sp->getSize());
                    //spawn.setLife(it->getLife());

                    random = ((double) rand() / (RAND_MAX+1));
                    randomTwo = ((double) rand() / (RAND_MAX+1)) * 7;
                    spawn = createAsteroid(spawn);

                    x_speed_asteriod = (spawn.getXDirection())*(spawn.getRandomVelocity());// + x_speed_asteriod;
                    y_speed_asteriod = (spawn.getYDirection())*(spawn.getRandomVelocity());// + y_speed_asteriod;
                    spawn.setXSpeed(x_speed_asteriod);
                    spawn.setYSpeed(y_speed_asteriod);

                    if(spawn.getRandomAxis() == 0){
                        glRotatef(spawn.getAngleRotation(), 1, 0, 0);
                    }else if(spawn.getRandomAxis() == 1){
                        glRotatef(spawn.getAngleRotation(), 0, 1, 0);
                    }else if(spawn.getRandomAxis() == 2){
                        glRotatef(spawn.getAngleRotation(), 0, 0, 1);
                    }

                    //it = asteroids.begin() + asteroidCount;
                    //asteroids.insert(it, spawn);
                    //asteroids.resize(asteroidCount);
                    asteroids.push_back(*sp);

                    glPushMatrix();
                    glScalef(.1,.1,.1);
                    glTranslatef(spawn.getXPosition()-3, spawn.getYPosition()-3, 0);
                    spawn.display_asteriod(spawn.getSize(), random, randomTwo);
                    glPopMatrix();
                }else{
                    sp++;
                }

1 Answers1

0

Your iterator sp is getting invalidated by the call to push_back. You are modifying the asteroids vector but you are still using the old iterator that you obtained before the modification.

This post contains a summary of rules for when iterators are invalidated.

Keeping track of new items to work on is often done using a queue (or a deque) in a safe way like this:

#include<deque>   

vector<Asteroid> asteroids;

deque<Asteroid> asteroid_queue;
//add all current asteroids into the queue
asteroid_queue.assign(asteroids.begin(), asteroids.end());

while(!asteroid_queue.empty())
 {
   //grab the next asteroid to process
   Asteroid asteroid = asteroid_queue.front();
   //remove it from the queue
   asteroid_queue.pop_front();

   do_some_work()

   //if necessary add a new asteroid .. be careful not to end in an infinite loop
   asteroid_queue.push_back(..);
 }
Community
  • 1
  • 1
cyon
  • 8,624
  • 4
  • 18
  • 25