引发了异常: 读取访问权限冲突。
**this** 是 0xCCCCCCCC。 这是VS2017上调试显示的错误类型。
#include<iostream>
#include<string.h>
using namespace std;
class List {
public:
class Node {
private:
int data;
public:
void setdata(int x) {
data = x;
}
int getdata(void) {
int i = data;
return i;
}
Node(void) {
}
Node(int x) {
data = x;
}
Node *next = NULL;
Node *pre = NULL;
};
List(void);
~List(void);
List(const List&s);
Node* first=new Node;
Node* last=new Node;
/*在开头插入结点*/
void insertNode(int x) {
Node p;
p.setdata(x);
first->pre = &p;
p.next = first;
first =&p;
}
/*删除结点*/
void deleteNode(int x) {
Node *p=first;
if (first != NULL) {
if (x == first->getdata()) {
first = first->next;
first->pre = NULL;
delete p;
p = NULL;
}
else
for (; p != NULL; p = p->next) {
if (x == p->next->getdata()) {
Node *q = new Node;
q = p->next;
p->next = q->next;
q->next->pre = p;
delete q;
q = NULL;
break;
}
}
}
else {
cout << "链表为空" << endl;
}
}
/*查找结点*/
Node* selectNode(int x) {
Node* p = first;
if (first != NULL) {
for (; x != p->getdata(); p = p->next) {
if (p->next == NULL) {
cout << "无所查询结点" << endl;
return NULL;
}
}
return p;
}
else {
cout << "链表为空" << endl;
return p;
}
}
};
List::List(void) {
Node k(0);
first = &k;
last = &k;
}
List::~List(void) {
for (; first != NULL; ) {
Node *p = first;
first = first->next;
delete p;
}
cout << "链表已删除" << endl;
}
List::List(const List &s) {
first->setdata(s.first->getdata());
Node* p = s.first;
Node* q = first;
while (p->next) {
p = p->next;
Node c(p->getdata());
c.pre = q;
q->next = &c;
q = q->next;
}
last = q;
}
int main(void) {
List a;
a.insertNode(5);
cout << a.selectNode(5) << endl;
system("pause");
return 0;
}
这是代码。
我想要求助的是 为什么会出现这种异常类型?(这种异常类型是因为什么原理出现的) 为什么在 return i;语句中反馈这种异常?
希望
试了试在int data;时直接将data初始化为0,但是结果并没有任何改变。