抛出异常:读取访问冲突 this->pCurr 为 0xDDDDDDDD
Exception thrown: read access violation this->pCurr was 0xDDDDDDDD
我是初学者,正在研究 linked 列表。我正在尝试制作一种删除列表当前 link 的方法。我遇到异常:读取访问冲突 this->pCurr 为 0xDDDDDDDD。
这是我的代码的样子(我删除了一些方法,例如添加 links 并且只留下与删除相关的方法)
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--;
}
我是初学者,正在研究 linked 列表。我正在尝试制作一种删除列表当前 link 的方法。我遇到异常:读取访问冲突 this->pCurr 为 0xDDDDDDDD。
这是我的代码的样子(我删除了一些方法,例如添加 links 并且只留下与删除相关的方法)
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--;
}