Sign in to follow this  
geoteam

Deleteing inside a list using iterators

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
[tt]delete (&salvoit)[/tt] is almost certainly wrong. It tries to delete the iterator itself, not what the iterator is pointing to. Depending on the type of salvo [tt]delete *salvoit[/tt] 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 ->.

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this