这是我的代码,每当我运行时都会在运行时引发“迭代器不可递增”错误。如果我注释掉 sp++ 行或 asteroids.push_back(*sp); 线然后它运行良好。所以它与这些行有关...我在上一篇文章中看到 sp->getSize() 行也增加了指针,这可能是问题的原因?谢谢您的帮助!
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++;
}