由于指针导致的分段错误
我遇到了很多麻烦,因为我忘记了所有的指针规则。我三年前了解了指针,从那以后就没有使用过它们。我在 contents ->; 行中收到分段错误在LinkedList.cpp文件中添加函数中的setPrevious(&node)
。我相信它与调用 setPrevious 函数或将节点作为指针传递有关。任何帮助都会很棒。谢谢!
LinkedList.h
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinkedList.cpp
#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"
using namespace std;
//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
count = 0;
contents = NULL;
}//ends the constructor
//adds an element to the front of the linked list
void LinkedList::add(int element)
{
int found = 0, current = 0;
for (int index = 0; index < count; index++)
{
if (contents -> getElement() == element)
found = 1;
else
{
contents = contents -> getNext();
}//ends the else statement
}//ends the while loop
if ((found == 0) && (count == 0))
{
LinearNode node;
node.setElement(element);
contents = &node;
count++;
print();
}//ends the if statement
else
{
LinearNode node;
node.setElement(element);
node.setNext(contents);
contents -> setPrevious(&node);
contents = &node;
count++;
//print();
cout << endl;
}//ends the found == 0 if statment
}//ends the add function
//this function removes one element from the linked list.
int LinkedList::remove(int element)
{
int found = 0, result = 0;
LinearNode* previous;
LinearNode* current;
if (count == 0)
cout << "The list is empty" << endl;
else
{
if (contents -> getElement() == element)
{
result = contents -> getElement();
contents = contents -> getNext();
}//ends the contents.getElement() == element
else
{
previous = contents;
current = contents -> getNext();
for (int index = 0; ( (index < count) && (found == 0) ); index++)
if (current -> getElement() == element)
found = 1;
else
{
previous = current;
current = current -> getNext();
}//ends the else statement
if (found == 0)
cout << "The element is not in the list" << endl;
else
{
result = current -> getElement();
previous -> setNext(current -> getNext());
}//ends else statement
}//ends the else stamtement
count--;
}//ends the else statement of count == 0
return result;
}//ends the remove function
void LinkedList::print()
{
LinearNode* current;
current = contents;
for (int index = 0; index < count; index++)
{
cout << current -> getElement() << endl;
current = current -> getNext();
}//ends the for loop
}//ends Print function
LinearNode.h
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinearNode.cpp
#include<iostream>
#include"LinearNode.h"
using namespace std;
//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
next = NULL;
element = 0;
}//ends LinearNode default constructor
//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
next = NULL;
previous = NULL;
element = el;
}//ends LinearNode constructor
//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
return next;
}//ends getNext function
//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
return previous;
}//ends getPrevious function
//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
next = node;
}//ends the setNext function
//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
previous = node;
}//ends the setPrevious function
//returns element of the node
int LinearNode::getElement()
{
return element;
}//ends the getelement function
//sets the element of the node
void LinearNode::setElement(int el)
{
element = el;
}//ends the setElement function
I have been having a ton of trouble because I have forgotten all of the rules of pointers. I learned about pointers 3 years ago and haven't used them since. I am receiving a segmentation fault in the line contents -> setPrevious(&node)
in the function add in the LinkedList.cpp file. I believe it has something to do either with calling the setPrevious function or passing node as a pointer. Any help would be great. Thanks!
LinkedList.h
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinkedList.cpp
#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"
using namespace std;
//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
count = 0;
contents = NULL;
}//ends the constructor
//adds an element to the front of the linked list
void LinkedList::add(int element)
{
int found = 0, current = 0;
for (int index = 0; index < count; index++)
{
if (contents -> getElement() == element)
found = 1;
else
{
contents = contents -> getNext();
}//ends the else statement
}//ends the while loop
if ((found == 0) && (count == 0))
{
LinearNode node;
node.setElement(element);
contents = &node;
count++;
print();
}//ends the if statement
else
{
LinearNode node;
node.setElement(element);
node.setNext(contents);
contents -> setPrevious(&node);
contents = &node;
count++;
//print();
cout << endl;
}//ends the found == 0 if statment
}//ends the add function
//this function removes one element from the linked list.
int LinkedList::remove(int element)
{
int found = 0, result = 0;
LinearNode* previous;
LinearNode* current;
if (count == 0)
cout << "The list is empty" << endl;
else
{
if (contents -> getElement() == element)
{
result = contents -> getElement();
contents = contents -> getNext();
}//ends the contents.getElement() == element
else
{
previous = contents;
current = contents -> getNext();
for (int index = 0; ( (index < count) && (found == 0) ); index++)
if (current -> getElement() == element)
found = 1;
else
{
previous = current;
current = current -> getNext();
}//ends the else statement
if (found == 0)
cout << "The element is not in the list" << endl;
else
{
result = current -> getElement();
previous -> setNext(current -> getNext());
}//ends else statement
}//ends the else stamtement
count--;
}//ends the else statement of count == 0
return result;
}//ends the remove function
void LinkedList::print()
{
LinearNode* current;
current = contents;
for (int index = 0; index < count; index++)
{
cout << current -> getElement() << endl;
current = current -> getNext();
}//ends the for loop
}//ends Print function
LinearNode.h
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinearNode.cpp
#include<iostream>
#include"LinearNode.h"
using namespace std;
//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
next = NULL;
element = 0;
}//ends LinearNode default constructor
//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
next = NULL;
previous = NULL;
element = el;
}//ends LinearNode constructor
//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
return next;
}//ends getNext function
//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
return previous;
}//ends getPrevious function
//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
next = node;
}//ends the setNext function
//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
previous = node;
}//ends the setPrevious function
//returns element of the node
int LinearNode::getElement()
{
return element;
}//ends the getelement function
//sets the element of the node
void LinearNode::setElement(int el)
{
element = el;
}//ends the setElement function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会在堆栈上创建一个 LinearNode,使
contents
指向此节点,将范围保留在以下}
- 这会使node
无效 - 和contents 指向无效数据。
您需要重新考虑您的整个类 - 链接列表需要堆存储,因此您必须使用
new
和delete
您的源代码中还有其他几个错误,但您应该修复此问题首先提出基本的误解,然后在需要时提出更新的问题。
其他一些错误:
This creates a LinearNode on stack, makes
contents
point to this node, leaves the scope at the following}
- which invalidatesnode
- andcontents
thereafter points to invalid data.You need to rethink your whole class - a linked list requires heap storage, so you have to use
new
anddelete
There's several other errors in your source, but you should fix this basic misconception first, and then come back with an updated question if needed.
Some of the other errors:
问题是您没有在堆中分配 Node,而是在堆栈上分配 Node。
在add函数中
应该是:
The problem is that you are not allocating Node in the heap, only on the stack.
In the add function
Should be:
寻找每个小问题需要花费大量代码,但其中跳出来的一件坏事是在 LinkedList::add() 中。在那里,您在堆栈上声明“节点”,然后设置一个指向它的指针。当 add() 返回时,指向的对象变成垃圾——析构函数被调用。这种情况很快就会导致段错误。
That's a lot of code to go through looking for every little problem, but one bad thing that jumps out is in LinkedList::add(). There you're declaring "node" on the stack, and then setting a pointer to point to it. When add() returns, that pointed-to object becomes trash -- the destructor is called. That's the kind of thing that leads quickly to segfaults.