【问题标题】:List iterator not incrementable when checking collision between objects [duplicate]检查对象之间的冲突时,列表迭代器不可递增[重复]
【发布时间】:2014-01-01 20:26:00
【问题描述】:

我目前正在用 cinder 编写一个太空射击游戏来学习 c++。我正在检查激光和敌人之间的碰撞。

void ParticleController::CheckCollisions()
{

  for(std::list<Enemy>::iterator e = enemys_.begin(); e != enemys_.end();)
  {
      for(std::list<LaserParticle>::iterator p = laserParticles_.begin(); p != laserParticles_.end();)
      {
          if(e->GetBoundingBox().intersects(p->GetBoundingBox()))
          {
              e = enemys_.erase(e);
              p = laserParticles_.erase(p);
          }

          else
            ++p;
      }   

      ++e;
  }
}

但我收到错误“列表迭代器不可递增”。我之前遇到过这个错误,但这次我似乎无法修复它。

【问题讨论】:

  • 当您尝试比 .end() 进一步增加时,您可能会收到此错误
  • @Elliot Robinson 该问题的答案不适用于我的情况
  • 这应该是所有敌人都被激光击中的情况下的精确应用,因为您的++e 不以e != enemys_.end() 为条件

标签: c++ cinder


【解决方案1】:

很可能发生在你刚刚消灭了最后一个敌人时。在这种情况下,erase 返回 end() 并且 e 不能再增加了。

if (e != enemys_.end())
  ++e;

【讨论】:

    猜你喜欢
    • 2010-09-16
    • 2023-03-03
    • 2011-09-04
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多