Error when trying to remove an object from a list while going through it in C++

Hi I am a beginner in c++ and I would like to know why this code returns a Debug Assertion Failed error everytime an object is erased from the list.

for (auto it = ProjectileList.end(); it != ProjectileList.begin();) {
            --it;
            if (it->position_y < 0) {
                ProjectileList.erase(it);
            }
            else {
                it->Draw(window.renderer);
                it->position_y--;
            }
        }


Read more here: https://stackoverflow.com/questions/67929889/error-when-trying-to-remove-an-object-from-a-list-while-going-through-it-in-c

Content Attribution

This content was originally published by Froncis at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: