由于指针的分割错误
我一直有很多麻烦,因为我忘记了所有的指针规则。我从3年前就了解了关于指针的知识,从那以后就没有用过它们。我在LinkedList.cpp文件的函数add中收到线contents -> 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
LinearNode node;
node.setElement(element);
contents = &node;
count++;
这将创建堆栈一个LinearNode,使得contents
点这个节点,叶范围在以下}
- 其中无效node
- 和contents
此后指向invali d数据。
你需要重新思考你的整个类 - 链表需要堆存储,所以你必须使用new
和delete
有没有在你的源等几个错误,但你应该先解决这个问题的基本误解,然后如果需要,请回来更新问题。
其他一些错误:解引用指针之前
- 拷贝构造函数,赋值运算符和析构函数
- 没有检查空的缺乏
- 没有设置所有的指针为NULL构造
好吧我修复它以便它的LinearNode * node = new LinearNode,但是我仍然遇到了分段错误。它发生在LinearNode.cpp文件中的previous = node时 – tpar44 2011-03-16 22:17:31
问题是,您没有在堆中分配节点,只能在堆栈中分配节点。
在添加功能
LinearNode node;
node.setElement(element);
contents = &node;
count++;
应该是:
LinearNode* node = new LinearNode;
node->setElement(element);
contents = node;
count++;
RAW指针是魔鬼玩具。不惜一切代价避免。 – 2011-03-16 21:11:21
这就是为什么我在大多数情况下使用python – fabrizioM 2011-03-16 21:17:49
这是很多代码去查找每一个小问题,但跳出来的一件坏事是在LinkedList :: add()中。你在堆栈中声明“节点”,然后设置一个指向它的指针。当add()返回时,该指向的对象变成垃圾 - 析构函数被调用。这种情况很快就会导致段错误。
您复制了Linearnode.h两次,我们在这里看不到LinkedList.h。 – 2011-03-16 21:00:45