C++链接列表运行时错误:未处理的异常 - 写入位置冲突
我正在尝试用 C++ 构建自己的链表实现。我的代码正在编译,但显然我的指针引用无效的内存地址存在一些问题。
这是我的实现:
#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
string _car;
Node* nextNode;
public:
void setCar(string car)
{
_car = car;
}
string getCar()
{
return _car;
}
void setNextNode(Node* node)
{
nextNode = node;
}
Node* getNextNode()
{
return nextNode;
}
};
Node* findLast(Node* node)
{
Node* nodeOut = NULL;
while (node->getNextNode() != NULL)
{
nodeOut = node->getNextNode();
}
return nodeOut;
}
string toString(Node* node)
{
string output = "";
while (node->getNextNode() != NULL)
{
output += node->getCar() + " ";
node = node->getNextNode();
}
return output;
}
int main()
{
char xit;
//ser head node to NULL
Node* headNode = NULL;
//create node 1
Node* node1 = new Node();
node1->setCar("Mercedes");
//create node 2
Node* node2 = new Node();
node2->setCar("BMW");
//set node links
headNode->setNextNode(node1);
node1->setNextNode(node1);
node2->setNextNode(node2);
headNode = node1;
Node* lastNode = findLast(headNode);
lastNode->setNextNode(NULL);
cout << toString(headNode) << endl;
//pause console
cin >> xit;
}
I am trying to build my own implementation of a linked list in C++. My code is compiling but apparently there is some issue with my pointers referring to invalid memory addresses.
Here is my implementation:
#include <iostream>
#include <string>
using namespace std;
class Node
{
private:
string _car;
Node* nextNode;
public:
void setCar(string car)
{
_car = car;
}
string getCar()
{
return _car;
}
void setNextNode(Node* node)
{
nextNode = node;
}
Node* getNextNode()
{
return nextNode;
}
};
Node* findLast(Node* node)
{
Node* nodeOut = NULL;
while (node->getNextNode() != NULL)
{
nodeOut = node->getNextNode();
}
return nodeOut;
}
string toString(Node* node)
{
string output = "";
while (node->getNextNode() != NULL)
{
output += node->getCar() + " ";
node = node->getNextNode();
}
return output;
}
int main()
{
char xit;
//ser head node to NULL
Node* headNode = NULL;
//create node 1
Node* node1 = new Node();
node1->setCar("Mercedes");
//create node 2
Node* node2 = new Node();
node2->setCar("BMW");
//set node links
headNode->setNextNode(node1);
node1->setNextNode(node1);
node2->setNextNode(node2);
headNode = node1;
Node* lastNode = findLast(headNode);
lastNode->setNextNode(NULL);
cout << toString(headNode) << endl;
//pause console
cin >> xit;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要重新查看您的代码。
头节点 = 节点1;
该赋值应该在访问实例 headNode 的任何成员函数之前完成。
最初,您已将 NULL 分配给该指针。
创建node1后,您将设置为无效实例的headNode。这就是崩溃的原因。
确保你的目标,然后尝试在纸上做一些粗略的工作,制作一些图表,这样你会更清楚你到底想要实现什么。
为什么要设置NextNode???我不明白你想达到什么目的。首先要明确。
根据我的理解,这段代码应该像下面这样实现。
希望它对在 C++ 中实现链接列表的初学者有用。
You need to relook at your code.
headNode = node1;
This assignment should be done before accesing any member function of the instance headNode.
Intially you have assigned NULL to this pointer.
After creating node1 you are setting to headNode that is invalid instance. This is the cause of crash.
Be ensure with your objective and then try to implement do some rough work on paper , make some diagram that way you would be more clear that what you are exactly trying to achive.
why setNextNode ??? i don't undeerstand what you wanted to achieve. be clear first.
As per my undertanding this code should be implemented like below..
Hope it would be useful for the beginner who implement ing the linklist in c++.
重读一下:
......并想想你在这里做什么。
如果您要编写链表代码,我建议至少查看
std::list
的接口。现在,您的接口处于如此低的级别,以至于您至少可以直接操作指针。Reread this:
...and think about what you're doing here.
If you're going to write linked-list code, I'd advise at least looking at the interface for
std::list
. Right now, you're interface is at such a low level that you'd be at least as well off just manipulating pointers directly.实际错误的原因是:
headNode
仍设置为 NULL,因此您取消引用 NULL 指针。正如 Jerry 所指出的,您还要求节点指向自身,这不是您想要的。如果将汽车作为构造函数参数会更清晰。
The cause of your actual error is:
headNode
is still set to NULL, thus you're dereferencing a NULL pointer. As noted by Jerry, you're also calling having nodes point to themselves, which is not what you want.It would be cleaner if you took the car as a constructor parameter.
当您分配一个新的
Node
时,指针nextNode
并未初始化,它只是随机垃圾。您需要将其显式设置为 NULL(可能在 Node 的构造函数中)。另外,我假设您知道标准 C++ 库有一个内置的链接列表,您这样做只是为了学习;-)
When you allocate a new
Node
, the pointernextNode
is not initialized, it's just random junk. You will need to explicitly set it toNULL
(probably in a constructor forNode
).Also, I assume you know that the standard C++ library has a linked list built in and you're just doing this for learning ;-)
感谢您的所有建议,这是我在主要清理后的最终代码:
Thanks for all the suggestions, here is my final code after major cleanup: