Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:23:47+00:00 2026-05-28T19:23:47+00:00

for(it = gameObjects.begin();it!=gameObjects.end();it++){ it->second->update(frameTime); if(it->second->getSprite()->GetPosition().y > 500){ std::cout << Removing enemy << std::endl; std::map<sf::String,VisibleGameObject*>::iterator

  • 0
for(it = gameObjects.begin();it!=gameObjects.end();it++){
    it->second->update(frameTime);
    if(it->second->getSprite()->GetPosition().y > 500){
        std::cout << "Removing enemy" << std::endl;
        std::map<sf::String,VisibleGameObject*>::iterator itor = Remove(it->second->getName());
        if(itor!=gameObjects.end()){
            std::cout << "itor doesn't equal" << std::endl;
            it=itor;
        }else{
            std::cout << "itor = end" << std::endl;
            it=itor;
        }
    }
}

As soon as itor = end is printed, it errors – “map set iterator not incrementable”. I thought the for loop should end before it increments again, as it!=gameObjects.end() will be false after this. Adding a break in the else statement resolves the problem.

Why doesn’t it work without the break? I’m assuming it’s something to do with when the iterator is incremented compared to when the condition is checked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:23:48+00:00Added an answer on May 28, 2026 at 7:23 pm

    You assume correct. The iterator is incremented at the end of the loop, and then the condition is checked.

    So after “itor = end” is printed, it gets incremented to gameObjects.end()++ which of course is not valid. You could get around by checking for itor == gameObjects.end() inside the loop, and then breaking.

    EDIT: As pointed out in the comments, you’re better just removing the ++it from the loop, to avoid skipping over the element after a removed element. For example:

    for( it = gameObjects.begin(); it!=gameObjects.end(); ) {
        ...
        if(it->second->getSprite()->GetPosition().y > 500) {
            it = Remove( it->second->getName() );
        } else {
            ++it;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

These lines of code are run: Enemy* enemy = new Enemy(m_pSceneManager,enemy+ss.str()); std::cout << Enemy
gameObjects is a std::map<sf::String,VisibleGameObject*> , and results is a std::map<sf::String,VisibleGameObject*>::iterator . When this runs:
I'm developing game. I store my game-objects in this map: std::map<std::string, Object*> mObjects; std::string
I have a C++ std::vector denoted as: std::vector<GameObject*> vectorToSort; Each object in vectorToSort contains
Lets say you make a game engine, and you have several GameObjects and every
For the below code snippet, how do I initialize instances of class Enemy with
In my game, all game objects extend an Entity class. All enemy objects have
What would be a better parctice, writing the drawing method inside the GameObject class
I realise this may be a duplicate, but I have searched through many forums
Note that the order can go either way (erase first then push back, just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.