Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Deleteing inside a list using iterators

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 geoteam   Members   

113
Like
0Likes
Like

Posted 29 January 2013 - 08:36 AM

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.



#2 SiCrane   Moderators   

11759
Like
4Likes
Like

Posted 29 January 2013 - 08:40 AM

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.

#3 Milcho   Members   

1177
Like
2Likes
Like

Posted 29 January 2013 - 08:48 AM

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



#4 SiCrane   Moderators   

11759
Like
2Likes
Like

Posted 29 January 2013 - 08:51 AM

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

Or a class that overloads operator ->.

#5 Milcho   Members   

1177
Like
1Likes
Like

Posted 29 January 2013 - 09:12 AM

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



#6 geoteam   Members   

113
Like
0Likes
Like

Posted 29 January 2013 - 09:25 AM

Thanks you guys it works fine now






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.