C++ 带指针的分段错误

C++ 带指针的分段错误,c++,pointers,segmentation-fault,C++,Pointers,Segmentation Fault,由于LinearNode.cpp(在setPrevious函数中读取previous=node的行)中的赋值操作,我遇到分段错误。Previous声明为LinearNode*Previous我真的需要一些帮助,因为我已经三年没有使用指针了,而且记不住太多。谢谢 LinearNode.cpp #include<iostream> #include"LinearNode.h" using namespace std; //Constructor for LinearNode, s

由于LinearNode.cpp(在setPrevious函数中读取
previous=node
的行)中的赋值操作,我遇到分段错误。Previous声明为
LinearNode*Previous我真的需要一些帮助,因为我已经三年没有使用指针了,而且记不住太多。谢谢

LinearNode.cpp

  #include<iostream>
#include"LinearNode.h"

using namespace std;

//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
    next = NULL;
    previous = 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)
{
cout << "next" << node -> getElement() << endl;
    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
#包括
#包括“LinearNode.h”
使用名称空间std;
//LinearNode的构造函数,将next和元素设置为初始化状态
LinearNode::LinearNode()
{
next=NULL;
previous=NULL;
元素=0;
}//结束LinearNode默认构造函数
//LinearNode的构造函数将元素作为参数。
LinearNode::LinearNode(int-el)
{
next=NULL;
previous=NULL;
元素=el;
}//结束LinearNode构造函数
//返回结构中的下一个元素
LinearNode*LinearNode::getNext()
{
下一步返回;
}//结束getNext函数
//返回结构中的上一个元素
LinearNode*LinearNode::getPrevious()
{
返回上一个;
}//结束上一个函数
//设置节点的下一个变量
void LinearNode::setNext(LinearNode*节点)
{
下一个=节点;
}//结束setNext函数
//为节点设置上一个
void LinearNode::setPrevious(LinearNode*节点)
{
coutgetnext();
}//结束else语句
}//结束while循环
如果((发现==0)和(&(计数==0))
{
LinearNode*节点=新的LinearNode;
节点->设置元素(元素);
内容=节点;
计数++;
打印();
}//结束if语句
其他的
{
LinearNode*节点=新的LinearNode;
节点->设置元素(元素);
节点->设置下一步(内容);
contents->setPrevious(节点);
内容=节点;
计数++;
打印();
coutgetnext();
}//结束内容。getElement()==元素
其他的
{
先前=内容;
当前=内容->获取下一步();
对于(int index=0;((index获取元素()==元素)
发现=1;
其他的
{
先前=当前;
当前=当前->获取下一步();
}//结束else语句
如果(找到==0)
cout setNext(当前->getNext());
}//结束else语句
}//结束这段谈话
计数--;
}//结束count==0的else语句
返回结果;
}//结束移除函数
作废链接列表::打印()
{
线性节点*电流;
当前=内容;
for(int index=0;index
我不确定这是否回答了您的问题,但我确实注意到一个指针可能为空的地方,这可能会导致segfault:

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
cout << "next" << node -> getElement() << endl;
    previous = node;
}//ends the setPrevious function
//为节点设置上一个
void LinearNode::setPrevious(LinearNode*节点)
{

cout您正在修改
内容
,我认为这应该指向列表的最前面

contents = contents -> getNext();
当您迭代直到
内容
为空时(在
添加()
),然后调用任何认为
内容
为非空的内容,因为
计数>0
会导致崩溃

第一次调用
add()
:计数为0,创建
LinearNode*
并正确设置
内容


第二次调用
add()
:count是1,通过执行
contents=contents->getNext()
-扫描,结果是
contents==null
。然后,由于
(found==0)
(count!=0)
创建一个新节点,并调用
contents->setPrevious()

你确定作业是问题发生的地方吗?你是否使用了调试器来得出这个结论?如果是,你能提供它发生的行号吗?我没有使用调试器,但当我
作业后不能
时,它不能正常工作,请记住
不能
是缓冲的-所以它是可能您看到的输出是旧的,因为在崩溃时缓冲区没有刷新。它肯定不是空的,因为我已经尝试过这个确切的方法,其中有一个整数,但是您确定每次调用此函数时,
node
都不是空的吗?是的,因为我刚刚添加了
if node!=null
,它仍然是空的seg faultedHmm…因此
节点
必须指向非空位置,但某个位置不是有效地址。您能否打印出
节点
的值(即地址)看看它崩溃时指向何处?好的,那么如果你将它与其他迭代中
节点的地址相比较,它有意义吗,或者地址方式不同?
contents = contents -> getNext();