Deleteing inside a list using iterators

Started by
4 comments, last by geoteam 11 years, 2 months ago

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.

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.

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 :)

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

Or a class that overloads operator ->.

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! ;)

Thanks you guys it works fine now

This topic is closed to new replies.

Advertisement