【发布时间】:2017-02-24 05:12:03
【问题描述】:
bool StudentList::remove(const char * studentName)
{
for (int i = 0; i < MAX_SIZE; i++)
{
if (this->students[i]->isEqualTo(studentName)) // Finds a name to remove
{
cout << "Remove: "; // Displays name wished to be removed
students[i]->print();
// delete[] students[i]; - Crashes
// students[i] = NULL; - Replaces removed name with null, stops working.
// students[i]->~Student(); - Call deconstructor, Crashes.
return true;
}
}
return false;
}
我只想从数组中删除一个元素,但是当我删除该元素时一直崩溃。
students[i] 是一个指针数组,我需要移除选中的元素
【问题讨论】:
-
查看this old SO question中提供的解决方案。
-
我们需要查看
StudentList::students的定义(它是一个数组、一个指针、一个unique_ptrs 的向量还是什么?)以及基本类型)。我们还需要知道“停止工作”是什么意思,以及何时代码崩溃。如果我们没有minimal reproducible example,这可能会被关闭。 -
最后的想法,你需要阅读 Eric Lippert 的 How to debug small programs。