What i'm trying to do here is to compare two lists of structures like this one below. And if two persons share at least for example 3 intrests they should be paired together and put into list of pairs. I'm starting with the first girl in the list and compare it to the boys, if a pair is found it puts them in a a pairlist and remove them from their respective boylist/girllist.
struct Person {
char name[30];
enum gendertype gender;
TableOfIntrests intrests; //The tableofintrests consists of an array with 6 containters representing six diffrent intrests.
};
Anyway, the problem I'm having is that the program works maybe ~50% of the times matching the persons and creating pairs. The other ~50% the I get an error message saying "List iterator not dereferancable". I have google the error message but I can't figure out what to do. Maybe I'm thinking completely wrong or it can be done in a much better way, I don't know but any feedback is appreciated.
void pair_together(Personlist *girllist, Personlist *boylist, Pairlist *pairlist, int least_number_of_intrests)
{
int equal_intrests = 0;
Pair pair;
Person p, p2;
int testcount3=0;
std::list<Person>::iterator i = girllist->begin();
std::list<Person>::iterator end = girllist->end();
std::list<Person>::iterator i2 = boylist->begin();
std::list<Person>::iterator end2 = boylist->end();
while ((i != end))
{
testcount3=0;
if(i2==end2)
break;
equal_intrests = number_of_equal_intrests(i->intrests, i2->intrests); //number_of_equal_intrests return the number of intrests that the two persons shares.
if(equal_intrests >= least_number_of_intrests)
{
printf("%s + %s, ", i->name, i2->name);
printf("%d\n", equal_intrests);
equal_intrests =0;
create_person(&p, i->name, i->gender);
create_person(&p2, i2->name, i2->gender);
create_pair(&pair, p, p2);
pairlist->push_back(pair);
i =girllist->erase(i);
i2 =boylist->erase(i2);//--
i2=boylist->begin();
testcount3=1;
}
else if(testcount3!=1)
{
i2++;
}
if((i2==end2) && (equal_intrests < least_number_of_intrests))
{
i++;
i2=boylist->begin();
}
if(number_of_intrests(i->intrests) <least_number_of_intrests)//number_of_intrests returns how many intrests a person have, so if the person have less intrests than least_number_of_intrests the program just skips to the next person.
{
i++;
}
}
}