Jump to content
  • Advertisement
Sign in to follow this  
geoteam

Deleteing inside a list using iterators

This topic is 1969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help, when i am looping through my list using iterators i need them delete themselves when they leave the screen my code is

for(salvoit = salvo.begin(); salvoit != salvo.end(); )
        {
            if((*salvoit)->x < 0 || (*salvoit)->x > 640)
            {
                delete (&salvoit);
                salvoit = salvo.erase(salvoit);
            }
            else
            {
                salvoit++;
            }
        }

 

whenever i run this it gives me an error when it tries to delete the object in the list. salvo is the list and salvoit is the iterator.

Share this post


Link to post
Share on other sites
Advertisement
delete (&salvoit) is almost certainly wrong. It tries to delete the iterator itself, not what the iterator is pointing to. Depending on the type of salvo delete *salvoit may be more appropriate.

Share this post


Link to post
Share on other sites

He must have pointers stored in there because

(*salvoit)->x
 

equates to dereference the iterator, which returns the element stored in the list. If the -> operator compiles, then the member must be a pointer.

 

Basically, what SiCrane said should fix it :) 

Share this post


Link to post
Share on other sites

If the -> operator compiles, then the member must be a pointer.

Or a class that overloads operator ->.

Share this post


Link to post
Share on other sites

If the -> operator compiles, then the member must be a pointer.

Or a class that overloads operator ->.

I realized that moments after posting, but was too lazy to edit! I should've known better that someone point that out! ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!