C++ | List iterator not incrementable -


i trying iterate through list , then, if object's plate number matches 1 given through parameters, , if toll (calculated in toll()) less or equal given cents, remove/erase object list. keep getting error list iterator cannot incremented , i'm clueless how fix it.

void one_time_payment(string& plate_number, int cents) {     // todo: rewrite function     std::list<licensetrip>:: iterator it;     (it = listlicense.begin(); != listlicense.end(); std::advance(it, 1)) {         if (it->plate_number().compare(plate_number) == 0) {             cout << "matching plate found" << endl;             if (it->toll() <= cents) {                 cout << "can paid" << endl;                 = listlicense.erase(it); //error: list iterator cannot incremented             }            }     }     cout << "end of iterator" << endl; } 

this is, i'm guessing, not compile error rather assertion triggered. have bug!

let's you're on last element, , conditions apply. do:

it = listlicense.erase(it);  

now, it end(). right after that, @ end of body of loop, advance it! undefined behavior! hence: list iterator cannot incremented.

to write correctly, there list::remove_if:

listlicense.remove_if([&](const licensetrip& trip){     return trip.plate_number() == plate_number &&         trip.toll() <= cents; }); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -