简体   繁体   English

链表分割错误

[英]Linked List Segmentation Fault

Why does this cause a SegFault Error? 为什么这会导致SegFault错误? I've tried to run a backtrace with gdb, but it has given me no help. 我试图用gdb运行回溯,但是它没有给我任何帮助。 Any help would be appreciated, I've been pulling my hair out over this for hours. 任何帮助将不胜感激,我已经在此工作了几个小时了。

my node.h 我的node.h

#ifndef NODE_H
#define NODE_H

#include <string>
using namespace std;

class Node
{
  public:

    Node(const string, const int) ;
   ~Node() { }
    void setNext(Node *);//setter for the next variable
    Node * getNext();// getter for the next variable
    string getKey();// getter for the key variable
    int getDistance();   // getter for the dist variable

  private:
    Node *next;
    int dist;
    string key;
};

#endif

My Node.cpp 我的Node.cpp

#include "node.h"
#include <string>

Node::Node(string k, int d){
    key = k;
    dist = d;
}

void Node::setNext(Node * n){
    next = n;
}

Node * Node::getNext(){
    return next;
}

string Node::getKey(){
return key;
}

int Node::getDistance(){
    return dist;
}

My list.h 我的清单

#ifndef LIST_H
#define LIST_H

#include "node.h"

class SLL
{
    public:
        SLL();
        ~SLL() { }
               void Insert (string searchKey, int distance);
               bool Delete (string searchKey);
               void Print();
               int Search(string searchKey);

    private:
        int count;
        Node *head;
    Node *iterator;
    Node *temp;
};

#endif

my List.cpp 我的List.cpp

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

SLL::SLL():head(0){}

void SLL::Insert(string searchKey, int distance){
Node * temp = new Node(searchKey, distance);

if(head == 0){
    head = temp;
}
else{
    temp->setNext(head);
    head = temp;
}
}

bool SLL::Delete(string searchKey){
 if(head == 0){
cout << "An attempt was made to delete a node from an empty list" << endl;
 }
 else{
Node* iterator = head;
Node* last = 0;

while(iterator != 0){
   if (iterator->getKey() == searchKey){
    break;
   }
   else{
    last = iterator;
    iterator = iterator->getNext();
   }
}
if (iterator == 0){
    return false;
}
else{
    if(head == iterator){
        head = head->getNext();

    }
    else {
        last->setNext(iterator->getNext());
    }
    delete iterator;



    }

    }
 }

void SLL:: Print(){
iterator = head;
while(iterator != 0){   
    cout << iterator->getKey()  << "-" << iterator->getDistance() << endl;
    iterator = iterator->getNext();
 }

}

int SLL::Search(string searchKey){

 }

My main.cpp 我的main.cpp

#include "list.h"
#include "node.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1);
    sll->Insert("test2", 2);
    sll->Delete("test");
    sll->Print();
}

Hint: Segfault happens here: 提示:分段错误发生在这里:

int main(int argc, char* argv[]) {
    SLL * sll; 

    sll->Insert("test", 1); // BIG segfault here.
    ...

(No full answers as this looks like homework.) (没有完整的答案,因为这看起来像是家庭作业。)

In your main function, the pointer to SSL is not initialised, but you dereference it. 在您的主要功能中,指向SSL的指针未初始化,但您取消了对其的引用。 This is undefined behaviour. 这是未定义的行为。 In your particular case, this is causing a segmentation violation. 在您的特定情况下,这会导致细分冲突。 Try changing you code to create a SSL object, either on the stack: 尝试更改您的代码以在堆栈上创建一个SSL对象:

int main(int argc, char* argv[]) {
    SLL sll;

    sll.Insert("test", 1);
    // ...
}

or the heap: 或堆:

int main(int argc, char* argv[]) {
    SLL * sll = new SLL();

    sll->Insert("test", 1);
    // ...
}

BTW, you are never using the temp , iterator , ... fields of the SLL class, never initialise them. 顺便说一句,您永远不要使用SLL类的tempiterator ,...字段,也不要初始化它们。 In your implementation, you define local variables that hide them, so I'd suggest removing the fields or initialising them in the constructor. 在实现中,您定义了隐藏它们的局部变量,因此建议您删除字段或在构造函数中对其进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM