C++ 访问冲突读取位置:链表c++;
我正在尝试做一个int值的链表。我将3个int值添加到列表中,然后打印它们,但在打印3个值后,我的问题是,程序返回到print函数以打印第4个值,因为(tempNode!=NULL)给出true,但在打印3个值后它应该为NULL,因此,它在C++ 访问冲突读取位置:链表c++;,c++,linked-list,nodes,C++,Linked List,Nodes,我正在尝试做一个int值的链表。我将3个int值添加到列表中,然后打印它们,但在打印3个值后,我的问题是,程序返回到print函数以打印第4个值,因为(tempNode!=NULL)给出true,但在打印3个值后它应该为NULL,因此,它在cout处的print方法中给了我访问冲突读取错误next节点的next参数将其next成员隐藏起来,因此next=next正在分配给该参数。 重命名其中一个 另外,在打印时不要修改大小。请您将其简化为一个最小但完整的示例,以演示您遇到的问题?最好是我们可以编
cout处的print方法中给了我访问冲突读取错误next
节点的next
参数将其next
成员隐藏起来,因此next=next
正在分配给该参数。
重命名其中一个
另外,在打印时不要修改大小。请您将其简化为一个最小但完整的示例,以演示您遇到的问题?最好是我们可以编译和运行而不必修改它。我可以提供我的头文件,可以吗?如果您可以编写一个完整的程序,包括一个main()
函数,不超过50行,它可以复制您遇到的问题。这将使我们更容易帮助你。作为一种有益的副作用,你甚至可能会发现自己这样做的问题。
IntList::IntList()
{
first = NULL;
last = NULL;
size = 0;
}
IntList::Node::Node(const int& info, Node* next = NULL)
{
num = info;
next = next;
}
IntList::~IntList()
{
Node* tempNode = first;
while ( tempNode != NULL )
//for(int i = 0; i < size; i++)
{
Node* nextNode = tempNode->next;
delete tempNode;
tempNode = nextNode;
}
first = last = NULL;
size = 0;
}
IntList::IntList(const IntList& wl)
{
cout << "here word list copy conts " << endl;
first = last = NULL;
size = wl.size;
if(wl.first != NULL){
Node* tempNode = wl.first;
for(int i = 0; i < wl.size; i++)
{
addLast(tempNode->num);
tempNode = tempNode->next;
}
}
}
IntList& IntList::operator = (const IntList& wl)
{
cout << "here word list =" << endl;
if(this == &wl)
return *this;
Node* tempNode = first;
while ( tempNode != NULL )
{
Node* nextNode = tempNode->next;
delete tempNode;
tempNode = nextNode;
}
first = NULL;
last = NULL;
if(wl.first != NULL)
{
for(int i = 0; i < wl.size; i++)
{
addLast(tempNode->num);
tempNode = tempNode->next;
size++;
}
}
return *this;
}
void IntList::addFirst(int& winfo)
{
Node* firstNode = new Node(winfo);
//Node firstNode(winfo);
if(first == NULL)
{
first = last = firstNode;
}
else
{
firstNode->next = first;
first = firstNode;
}
//increment list size
size++;
}
void IntList::print(ostream& out)
{
Node* tempNode = first;
while ( tempNode != NULL )
{
out << "\t";
cout << "index " << size-- << ", value: "<< tempNode->num << endl;
tempNode = tempNode->next;
}
}