Jump to content

  • Log In with Google      Sign In   
  • Create Account


Deleteing inside a list using iterators


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   -  Reputation: 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.



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9496

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   Crossbones+   -  Reputation: 1175

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   -  Reputation: 9496

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   Crossbones+   -  Reputation: 1175

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   -  Reputation: 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.



PARTNERS