2013-02-15 194 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