抛出异常错误
Exception thrown error
我试图找出异常在哪里,我不能真正在我的程序中找出它。程序列表所做的是查找节点并删除它们。这是我的头文件:
#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H
#include <string>
#include <iostream>
using namespace std;
class Node
{
public:
Node() : data(0), previousLink(NULL), nextLink(NULL) {}
Node (int theData, Node *previous, Node *next)
: data(theData), previousLink(previous), nextLink(next){}
Node *getNextLink( ) const { return nextLink; }
Node *getPreviousLink( ) const { return previousLink; }
int getData( ) const { return data; }
void setData(int theData) { data = theData; }
void setNextLink(Node *newNext) { nextLink = newNext; }
void setPreviousLink(Node *newPrev) { previousLink = newPrev; }
~Node(){}
private:
int data;
Node *nextLink;
Node *previousLink;
};
class DoublyList
{
public:
DoublyList();
void insertBack(int newData);
bool search(int searchData) const;
void deleteNode(int deleteData);
void print() const;
void reversePrint() const;
void destroyList();
~DoublyList();
private:
Node *first; // pointer to the first node on the list
Node *last; // pointer to the last node on the list
int count; // number of nodes in the list
};
#endif
下面是cpp文件:
/*
Huynh, Dex
CS A250
October 9, 2016
Lab - 7 Dll Error
*/
#include "DoublyList.h"
DoublyList::DoublyList()
{
first = NULL;
last = NULL;
count = 0;
}
DoublyList::~DoublyList()
{
destroyList();
}
void DoublyList::insertBack(int newData)
{
Node *newNode = new Node(newData, last, NULL);
if (first == nullptr)
first = newNode;
else
{
last->setNextLink(newNode);
newNode->setPreviousLink(last);
}
}
bool DoublyList::search(int searchData) const
{
Node *current = first;
while (first != nullptr)
{
if (current->getData() == searchData)
return true;
else
current = current->getNextLink();
}
}
void DoublyList::deleteNode(int deleteData)
{
if (first == nullptr)
{
cerr << "Cannot delete from an empty list." << endl;
}
else
{
Node *current;
if (first->getData() == deleteData)
{
current = first;
first = first->getNextLink();
if (first == NULL)
last = NULL;
delete current;
current = NULL;
}
else
{
bool found = false;
current = first;
while (current != NULL || !found)
{
if (current->getData() == deleteData)
found = true;
else
current = current->getNextLink();
}
if (current == NULL)
cerr << "The item to be deleted is not in the list." << endl;
else
{
if (current != last)
current->getPreviousLink()->setNextLink(current->getNextLink());
else
last = current->getPreviousLink();
--count;
delete current;
current = NULL;
}
}
}
}
void DoublyList::print() const
{
if (first == nullptr)
cerr << "List is empty. Cannot print." << endl;
else
{
Node *temp = first;
while (temp != NULL)
{
cout << temp->getData() << " ";
temp = temp->getNextLink();
}
cout << endl;
}
}
void DoublyList::reversePrint() const
{
if (first == nullptr)
cerr << "List is empty. Cannot print." << endl;
else
{
Node *temp = last;
while (temp != NULL)
{
cout << temp->getData() << " ";
temp = temp->getPreviousLink();
}
cout << endl;
}
}
void DoublyList::destroyList()
{
Node *temp;
while (first != NULL)
{
first = temp;
temp = temp->getNextLink();
delete first;
first = NULL;
}
last = NULL;
count = 0;
}
Node *temp;
...
temp = temp->getNextLink();
在destroyList
中,此代码使用未初始化的值。程序的行为未定义
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 如何实现,错误分配中止而不是抛出异常
- 抛出异常:类型错误:对象不支持属性或方法"createVertexArray"(emscripten)
- 将索引超出数组边界在 C++ 中的内核之前抛出异常或错误
- emscripten + sdl = 抛出异常:类型错误:无法设置未定义的属性'widthNative'
- 队列用户APC - 抛出异常崩溃,可能是 mingw 错误
- X 的错误处理程序抛出异常是否安全?
- 为什么我不能从我的抛出异常中打印出错误?
- libc++ std::istringstream 不会抛出异常。错误?
- 抛出异常错误
- 从linux系统错误中抛出c++异常