删除函数正在使程序崩溃c ++
Remove Function is making the program to crash c++
/********************************************
* Remove the last employee from the list *
********************************************/
void EmployeeList::Remove()
{
newEmployee * nextToEnd = head,
* last = head->Next();
//THIS IS THE PROBLEM
//no nodes
if(head == NULL)
return;
//remove the only employee in the list
if(head->Next()== NULL)
{
cout << "nttEmployee ID " << head->empID() << " and salary $"
<< head->ySalary() << " have been removed.n";
head = NULL;
delete head;
}
else
{
// remove the last employee of the list
while(last->Next() != NULL)
{
nextToEnd = last;
last = last->Next();
}
cout << "nttEmployee ID " << last->empID() << " and salary $"
<< last->ySalary() << " have been removed.n";
delete last;
nextToEnd->SetNext(NULL);
}
}
尝试从空列表中删除时出现问题。我知道如果它是空的,我无法删除,但我不会崩溃程序以显示"员工列表为空。
我指定了我认为问题的位置,希望有人可以帮助我解决问题。
您所要做的就是代码中已经显示的内容。请注意它如何使用 cout
将文本输出到控制台。更改您指定"问题"的if
语句,即输出消息,然后返回。
但是您的程序崩溃与您标记的内容无关。它因为head->Next()
而崩溃。不能对NULL
的对象调用方法。这应该在if (head == NULL)
检查之后发生。
相关文章:
- 提升 ASIO Async_receive崩溃程序
- 类指针方法崩溃程序
- get_body来自 IHTMLDocument2 崩溃程序
- 是否有可能存在不会崩溃程序的内存问题
- 调试运行时出现奇怪的崩溃程序(Eclipse C++)
- 儿童对话框 - setWindowTexta或sendmessagea崩溃程序-MFC
- 顶点阵列GLFW崩溃C 程序
- 将值分配给Float数据类型崩溃程序
- OpenCV-将变量添加到类成员崩溃程序
- boost::interprocess::managed_shared_memory 崩溃程序
- 将枚举值分配给整数崩溃程序
- SDL 1.2 -> SDL 2.0(崩溃程序)
- 空析构函数崩溃程序:C++
- 冒泡排序崩溃程序c++
- Ofstream关闭崩溃程序
- 删除字符* 崩溃程序
- c++矢量擦除崩溃程序
- Lua_getglobal崩溃程序
- 通过Copy Constructor创建的e2打印崩溃程序
- RNG崩溃c++程序