簡體   English   中英

由於指針導致的分段錯誤

[英]Segmentation Fault because of Pointers

我一直遇到很多麻煩,因為我忘記了所有指針規則。 我3年前了解了指針,從那以后就再沒有使用過。 我在LinkedList.cpp文件中添加的函數的行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指向無效數據。

您需要重新考慮整個類-鏈表需要堆存儲,因此您必須使用newdelete

來源中還有其他幾個錯誤,但是您應該首先解決這個基本的誤解,然后在需要時再提出一個更新的問題。

其他一些錯誤:

  • 缺少復制構造函數,賦值運算符和析構函數
  • 在取消引用指針之前不檢查null
  • 沒有在構造函數中將所有指針設置為null

問題是您沒有在堆中分配Node,而僅在堆棧中。

添加功能

 LinearNode node;
 node.setElement(element);
 contents = &node;
 count++;

應該:

 LinearNode* node = new LinearNode;
 node->setElement(element);
 contents = node;
 count++;

尋找每個小問題都需要經過很多代碼,但LinkedList :: add()會跳出一件不好的事情。 在這里,您在堆棧上聲明了“節點”,然后設置了一個指向它的指針。 當add()返回時,指向的對象將變為垃圾箱-調用析構函數。 這種事情很快導致了段錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM