However if the "break;" line is added, everything Okay. Why is that?
Thank you very much!
list<Personel>::iterator iter;
for(iter=personList.begin(); iter!=personList.end(); iter++)
{
cout << (*iter).getName() << endl;
if((*iter).getID()==12345){
personList.erase(iter);
/*break;*/
}
}
Once you have removed 'iter' from the personList (with erase) you cannot
increment it and expect it to be valid anymore. Fortunatly, erase
returns an iterator for the next element, so for the above you would do
something like:
list<Personel>::iterator iter = personList.begin();
while ( iter != personList.end() )
{
cout << iter->getName() << endl;
if ( iter->getID() == 12345 )
iter = personList.erase( iter );
else
++iter;
}
--
Is your company in Tampa?
Improve it's understanding of OO.
Hire me... <http://home1.gte.net/danielt3/resume.html>
Thanks. That's very helpful!
Could someone tell me how to use remove() instead of erase() in this case?
Everytime compiling with an addition of a line,
"personList.remove(person3);", it gives me an error like:
"error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class Personel(or there is no acceptable conversion)"
Does anyone know what's wrong with using remove() above?
Thank again!
remove deletes items that are equal to the supplied parameter (person3 in
your case). Note the distinction, it doesn't delete person3, but any items
which are equal to person3.
But the compiler isn't psychic, you have to tell it what it means for one
person to be equal to another.
bool operator==(const Personel& x, const Personel& y)
{
// your code here, return true if x and y are equal, false if not
}
Given you previous code operator== is probably going to look something like
this
bool operator==(const Personel& x, const Personel& y)
{
// to people are the same if there ID's are the same
return x.getID() == y.getID();
}
john