【发布时间】:2016-05-20 10:45:52
【问题描述】:
我正在尝试使用 mylist.erase(); 删除链接列表中的节点,但该节点仍保留在列表中。我尝试使用 delete() 但程序崩溃了。任何的想法?
list <Person*> :: iterator it;
it = gamelist.begin(); //gamelist is a <Person*> list. it is an iterator to this list.
while (it!=gamelist.end()){
if ((*it)->is_dead == true) {
delete (*it); //if I comment this line the program does not crash but the "dead" Person still remains in the list.
it = gamelist.erase(it);
}
else ++it;
}
【问题讨论】:
-
您好,您是否尝试过 gamelist.erase 然后删除?
-
嗨@JuanPablo。是的,我试过了。同样的问题...
-
你确定你
newed 内存@*it并且它还没有被释放吗?您可以尝试将*it保存在临时的Person *中,然后擦除并删除临时的。 -
你为什么用
Person *而不是一些智能指针? -
欢迎使用 C++。仅仅因为某些代码块在某个特定点崩溃,并不意味着这正是错误所在。没有minimal reproducible example,就无法回答您的问题。
标签: c++ linked-list