简体   繁体   English

迭代器错误内的向量迭代器

[英]Vector Iterator within an iterator error

Working on an asteroid game for my portfolio. 正在为我的投资组合开发小行星游戏。 Game across an error with the following block of code. 使用以下代码块在错误中进行游戏。 The error is simply that the iterator is not deferencable, but I've narrowed the error down. 错误很简单,因为迭代器是不可延迟的,但是我缩小了范围。 I'll talk about that after I've shown you the code: 在向您展示代码之后,我将讨论这一点:

void Game::Update()
{
if (elapsedTime > REFRESH_RATE)
{
    player->Update(elapsedTime);

    std::vector<Asteroid*>::iterator iterator= bigAsteroids.begin();
    std::vector<Bullet*>::iterator iter = bullets.begin();

    // *********************** //
    while (iterator!= bigAsteroids.end())
    {
        if (*iterator != nullptr) // Checks to see if player is colliding with an asteroid and updates the asteroid
        {
            player->CheckCollisionsAsteroid( *iterator );

            // *********************** // Checks to see if bullet is colliding with asteroid 
            while (iter != bullets.end())
            {
                if (*iter != nullptr)
                {
                    if ((*iter)->CheckCollisionsAsteroid( **iterator ))
                    {
                        size_t i = iterator - bigAsteroids.begin();
                        iter = bullets.erase(iter);
                        iterator = bigAsteroids.erase(iterator);
                        printf("\nAsteroid destroyed at position %i", i);
                    }
                    else
                    {
                        ++iter;
                    }
                }
            }
            iter = bullets.begin(); // Reset the bullet iterator else we'd only get to check 1 asteroid

            // *********************** //
            if (*iterator != nullptr && iterator != bigAsteroids.end()) { (*iterator)->Update(elapsedTime); }
        }
        if (*iterator != nullptr && (iterator!= bigAsteroids.end()))
        {++iterator;}
    }
    // *********************** //
    Clock.Reset(); 
}
}

If it all looks a bit messy, here's a brief explanation: 如果看起来有些混乱,这里有一个简短的解释:

As the iterator passes through each of the asteroids, I check if the player is colliding with each of the asteroids. 当迭代器穿过每个小行星时,我检查玩家是否正在与每个小行星碰撞。 At present this does nothing 目前,这无能为力

I then iterate through my vector of bullets, checking each bullet against the current asteroid. 然后,我遍历子弹向量,并对照当前的小行星检查每个子弹。 if there is a collision, I destroy erase both the bullet AND the asteroid, and print out what position the destroyed asteroid held in the vector. 如果发生碰撞,我将消灭子弹和小行星,并打印出被摧毁的小行星在矢量中的位置。

Once I'm done with that, I reset the bullet iter for the next asteroid. 完成此操作后,我将重置下一个小行星的项目符号迭代器。

My major issue at present is the call to Update by the (*iterator) <-- asteroid. 我目前的主要问题是(* iterator)<-小行星对Update的调用。

The issue only crops up when I'm erasing the last object in the asteroids vector. 仅当我擦除小行星向量中的最后一个物体时,该问题才会出现。 I know erase returns an iterator to the next object - if the next object doesn't exist, an error will be thrown (as the iterator is then pointing to a nullptr, correct?). 我知道擦除会向下一个对象返回一个迭代器-如果下一个对象不存在,则会引发错误(因为迭代器然后指向nullptr,对吗?)。 I'm making the checks for a nullptr to no avail.. 我正在检查nullptr无济于事。

Does anyone have any advice or help they can offer? 是否有人可以提供任何建议或帮助? Would be much appreciated, been scratching my head for a good few hours! 非常感谢,,了好几个小时!

The problem is that when you destroy the last asteroid, iterator will be set to bigAsteroids.end() (by being assigned to the result of erase() ). 问题是,当销毁最后一个小行星时, iterator将设置为bigAsteroids.end() (通过分配给erase()的结果)。 Then after the inner while loop terminates, you dereference iterator . 然后,在内部while循环终止之后,您将取消引用iterator The past-the-end iterator cannot be dereferenced. 过去的迭代器无法取消引用。

You'll have to switch the order of checks: 您必须切换检查顺序:

if (iterator != bigAsteroids.end() && *iterator != nullptr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM