简体   繁体   中英

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.

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?). I'm making the checks for a nullptr to no avail..

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() ). Then after the inner while loop terminates, you dereference iterator . The past-the-end iterator cannot be dereferenced.

You'll have to switch the order of checks:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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