Cocos2d-x Vector--vector iterators Incompatible

Source: Internet
Author: User

*************************************** Reprint Please specify the Source:Http://blog.csdn.net/lttree ******************************************



When using vectors in the cocos2d-x,

There was an error when deleting an object, it crashed ...

vector<bullet*>* bullets;//iterates through each Bullet, letting them update for themselves (Auto it = Bullets->begin (); It!=bullets->end (); it++) {  (*it)->update ();  Get bullet life, if the bullet has died out, release  if ((*it)->getlife ()) {    bubblet* b = *it;    Bubblets->eraseobject (b);    This->removechild (b,true);  } }

There will be errors--vector iterators incompatible;

Maybe the way I opened it was wrong, so I used the C++11 method:

Vector<bullet*> bullets;for (auto& b:bullets)  {  b->update ();  if (B->getlife ())  {    bubblets.eraseobject (b);    This->removechild (b,true);  }}

or not ...

Looking for a long time, found that

It is said that because, when the iterator traversal, if the current to delete, then the back of the chaos, can not continue to go on,

So, it crashes.


So, if you traverse through an iterator, change it:

Iterate through each bullet and let them update for themselves (Auto it = Bullets->begin (); It!=bullets->end ();) {  (*it)->update ();  Get bullet life, if the bullet has died out, release  if ((*it)->getlife ()) {    bubblet* b = *it;    it = Bubblets->eraseobject (b);    This->removechild (b,true);  }  else  {    it++;  }}

the movement of an iterator is no longer a loop, but a judgment statement.


Unfortunately, through the c++11 method of traversal, I have not thought how to change ...







*************************************** Reprint Please specify the Source:Http://blog.csdn.net/lttree ******************************************

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2d-x Vector--vector iterators Incompatible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.