【发布时间】:2020-11-24 13:04:14
【问题描述】:
我一直在处理这个任务,并且遇到了这个我几个小时都无法修复的错误。它说抛出未处理的异常,读取访问冲突。我听说你不应该尊重 nullptrs,但我不相信这就是我正在做的事情。
void Linkedlist::insertnode(string key, string value) {
Node* newNode; //This points to the following node
Node* cursor; //This will traverse through list
newNode = new Node; //create a new node
newNode->data = new HashEntry(key, value); //Initialize the data
newNode->next = nullptr; //Leave the pointer for the node following after empty at inception;
if (!head) {
head = newNode; //if head is empty, this will make the first user input the head
return;
}
else {
cursor = head;
while (cursor->next)
;
//We'll traverse list to add newNode to tail
cursor->next = newNode; //for now the order doesn't matter since they're strings so we add new cluster to tail
}
}
【问题讨论】:
-
请发帖minimal reproducible example。您很可能通过
->取消引用它。 -
head在哪里初始化? -
while (cursor->next);是一个无限循环。 -
唯一可以为空的
this是Linkedlist自己的。你是如何创建这个实例的? -
请不要发文字图片,发文字。 (它 是
Linkedlist实例的this为空。您在某处使用了无效的Linkedlist*。假设问题的原因必然在附近,您可能已经陷入困境你发现它的地方。)
标签: c++ pointers nullpointerexception unhandled-exception nullptr