0

为什么这会导致 SegFault 错误?我试图用 gdb 运行回溯,但它没有给我任何帮助。任何帮助将不胜感激,我已经为此努力了好几个小时。

我的节点.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

我的节点.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;
}

我的清单.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

我的 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){

 }

我的 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();
}
4

2 回答 2

3

提示:Segfault 发生在这里:

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

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

(没有完整的答案,因为这看起来像家庭作业。)

于 2013-02-15T21:56:45.913 回答
1

在您的 main 函数中,指向的指针SSL未初始化,但您取消引用它。这是未定义的行为。在您的特定情况下,这会导致分段违规。尝试更改代码以在堆栈上创建SSL对象:

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

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

或堆:

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

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

顺便说一句,你永远不会使用类的temp, iterator, ... 字段SLL,永远不要初始化它们。在您的实现中,您定义了隐藏它们的局部变量,因此我建议删除这些字段或在构造函数中初始化它们。

于 2013-02-15T21:57:32.503 回答