2013-02-15 117 views
0

爲什麼會導致SegFault錯誤?我試圖用gdb運行一次backtrace,但它沒有給我任何幫助。 任何幫助,將不勝感激,我一直拉着我的頭髮超過這個小時。鏈接列表分段錯誤

我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 

我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; 
} 

我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 

我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(); 
} 
+2

gdb給你沒有幫助?!它應該告訴你你在哪裏得到段錯誤,並且你應該告訴我們。 – us2012 2013-02-15 21:54:35

+1

你認爲sll的初始值是什麼? – andre 2013-02-15 22:00:28

+0

除了導致已被回答的段錯誤的bug之外,您還有其他一些問題:1.看看您的ctor的Node。您似乎依賴於列表的下一個字段中最後一個元素爲0的事實,但您是否曾將其設置爲0? 2.避免在頭文件中使用語句,至少在全局範圍內使用。這是一個黃蜂巢等待發生(可能不是在一個硬件任務,但它是一個很好的習慣舉行)。請參閱http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c – eladidan 2013-02-15 22:16:42

回答

3

提示:段錯誤發生在這裏:(沒有完全的答案,因爲這看起來像功課)

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

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

1

在你的主函數,指針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); 
    // ... 
} 

順便說一句,你永遠不會使用tempiterator,...領域SLL類,從不初始化它們。在你的實現中,你定義了隱藏它們的局部變量,所以我建議刪除這些字段或者在構造函數中初始化它們。

+0

爲什麼使用指針來爲對象在堆棧解決方案?! – us2012 2013-02-15 21:58:50

+0

你說得對,固定使用'.'符號。我只是不想改變代碼結構太多。 – 2013-02-15 22:01:57

+0

非常感謝!這解決了這個問題,我不敢相信我忽略了這一點。 – user2073188 2013-02-15 22:06:07