引发异常:读取访问冲突 此>pCurr 已0xDDDDDDDD



我是一个初学者,正在处理链表。我正在尝试制作一个方法,删除列表的当前链接。我得到了一个异常:读取访问违规this->pCurr是0xDDDDDDDD。

这就是我的代码的样子(我删除了一些方法,比如添加链接,只留下了与删除相关的方法(

template <class T>
struct TNode {
T val;
TNode<T> *pNext;
};
template <class T>
class TList {
protected:
TNode <T> *pFirst, *pLast, *pCurr, *pPrev, *pStop;
int len; //lenght
int pos;//where pCurr shows
public:
//constructor
TList() {
pFirst = NULL;
pLast = NULL;
pCurr = NULL;
pPrev = NULL;
pStop = NULL;
len = 0;
pos = 0;
}
//destructor
~TList() {
TNode<T> *tmp = pFirst;
//if (tmp == pStop)
//delete tmp;
while (pFirst != pStop) {
pFirst = pFirst->pNext;
delete tmp;
tmp = pFirst;
}
}
//delete first link
void DelFirst() {
T res = pFirst->val;
TNode<T> *tmp;
tmp = pFirst;
pFirst = pFirst->pNext;
delete tmp;
len--;
}
//delete current link
void DelCurr() {
if (pCurr == pFirst)
DelFirst();
else {
TNode<T> *tmp = pCurr;
pPrev->pNext = pCurr->pNext; // an exception is thrown on this line
pCurr = pCurr->pNext;
delete tmp;
len--;
}
}
//set pCurr to the beginning
void Reset() {
pCurr = pFirst;
pPrev = pStop;
pos = 0;
}
//go to the next link
void GoNext() {
//if (IsEnd()) throw - 1;
//else {
pPrev = pCurr;
pCurr = pCurr->pNext;
pos++;
//}
}
//end check
bool IsEnd() {
return (pCurr == pStop);
}
//delete the whole list
void DelList() {
for (Reset(); !IsEnd(); GoNext()) {
DelCurr();
}
DelCurr();
}
};

请告诉我这个异常与什么有关,以及如何修复它。

检查各处是否有空指针

在DelFirst((函数中,当pCurr指向pFirst 时,将其设置为下一个

void DelFirst()
{
if (pFirst == NULL)
{
return;
}
if (pCurr == pFirst)
{
pCurr = pFirst->pNext;
}
T res = pFirst->val;
TNode<T>* tmp;
tmp = pFirst;
pFirst = pFirst->pNext;
delete tmp;
len--;
}

相关内容

  • 没有找到相关文章

最新更新