C++ List Insert Iterator -
a family object contains linked list of person objects, , such, function exists within family class insert person objects listedlist called people.
i’m having trouble person objects being added people linked list. have omitted number of other functions, (hopefully) able determine problem occurring within insertperson() function in family class.
the loop within insertpacket() looping through each ‘person’ object intended (refer commented ‘cout’). however, things break down in if statement..
if (person_id < itr->get_person_id()) { person *psn = new person(person_id, name); people.insert(itr, *psn); break; } if person_id less get_person_id() in linked list, executes , inserts @ beginning of list, if not, skips entirely...
you're fundamental bug here, in insertperson:
for(itr = people.begin(); itr != people.end(); itr++) { if (id < itr->get_person_id()) { people.insert(itr, person(id, text)); break; } } what if person's id greater all of current list members? insert never happen reach end() , break loop. should used find insertion point, can include end().
you can find proper insertion point doing this:
for(itr = people.begin(); itr != people.end(); itr++) { if (itr->get_person_id() < id) break; } people.insert(itr, person(id, text)); note will allow duplicate insertions given id. not sure if care that. finally, same problem exists insertion of family, want fix after this.
best of luck.
Comments
Post a Comment