Skip to main content
GameDev.net gamedev.net
🔒 Locked

Deleteing inside a list using iterators

Started by geoteam Jan 29, 2013 at 2:36 PM 4 replies 1.5k views
Original Post
geoteam
geoteam

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.

SiCrane
SiCrane
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.
MilchoPenchev
MilchoPenchev

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

SiCrane
SiCrane

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

Or a class that overloads operator ->.
MilchoPenchev
MilchoPenchev


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

geoteam
geoteam

Thanks you guys it works fine now

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.