Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STL list problem

1 view
Skip to first unread message

allenwu

unread,
Aug 24, 2002, 6:38:57 PM8/24/02
to
What's wrong with the folowing section of code? When I debugged throug it
line by line it shows "Access Violation" error at the "cout" line.

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;*/
}
}

Daniel T.

unread,
Aug 24, 2002, 7:24:22 PM8/24/02
to
In article <5WT99.2813$p%3.19...@bgtnsc05-news.ops.worldnet.att.net>,
"allenwu" <all...@hotmail.com> wrote:

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>

allenwu

unread,
Aug 24, 2002, 8:56:21 PM8/24/02
to
Daniel T. <notda...@gte.net> wrote in message
news:notdanielt3-37F9...@news.bellatlantic.net...

> 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;
> }

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!

John Harrison

unread,
Aug 25, 2002, 2:26:16 AM8/25/02
to

"allenwu" <all...@hotmail.com> wrote in message
news:VWV99.33143$Ke2.2...@bgtnsc04-news.ops.worldnet.att.net...

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

0 new messages