請教有關segmentation fault... [論壇 - Ubuntu 程式設計]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



請教有關segmentation fault...
會員一級
註冊日期:
2013/10/31 23:07
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 0
MP : 0 / 0
EXP: 0
離線
以下code在編譯後出現segmentation fault的問題
想請問各位大大錯誤的問題點在哪??
(個人找了很久還是找不出來..)

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

typedef int ElementType;
class LinkedList
{
private:
class Node
{
public:
ElementType data1;
ElementType data2;
ElementType data3;
Node * next;
Node(ElementType d1=0,ElementType d2=0,ElementType d3=0,Node * link=0)
{
data1 = d1;
data2 = d2;
data3 = d3;
next = link;
}
};
public:
typedef Node * NodePointer;
LinkedList();
~LinkedList();
void insert(ElementType,ElementType,ElementType,NodePointer);
NodePointer head();
void compare(NodePointer);
void display();
NodePointer first;
NodePointer tailptr;
private:
NodePointer predptr;
};

LinkedList::LinkedList()
{
first = new Node();
tailptr = new Node();
predptr = first;
first ->next = tailptr;
}

LinkedList::~LinkedList()
{
while(first->next != tailptr)
{
NodePointer ptr;
ptr = first->next;
first ->next = ptr->next;
delete ptr;
}
delete first;
delete tailptr;
}

void LinkedList::insert(ElementType d1, ElementType d2, ElementType d3, NodePointer predp)
{
NodePointer newptr= new Node(d1,d2,d3);
newptr->next = predp->next;
predp->next = newptr;

while(predptr->next !=tailptr)
{
predptr = predptr->next;
}

}

LinkedList::NodePointer LinkedList::head()
{
return first;
}

void LinkedList::compare(NodePointer origin)
{
if(origin == first)
{
origin = origin->next;
}

NodePointer ptr = origin->next;
if((origin->data2)-(ptr->data1)>0)
{
if((origin->data3) > (ptr->data3))
{
ptr->data1 = origin->data2;
}
else if((origin->data3) < (ptr->data3))
{
origin->data2 = ptr->data1;
}
else
{
origin->data2 = ptr->data2;
}
}
else if((origin->data2)-(ptr->data1) == 0)
{
if((origin->data3) == (ptr->data3))
{
origin->data2 = ptr->data2;
ptr->data1 = origin->data1;
}
}

}

void LinkedList::display()
{
NodePointer ptr = first;
while (ptr ->next != tailptr)
{
ptr = ptr ->next;
NodePointer tem = ptr->next;
if(tem->data1 == ptr->data1)
{
continue;
}
else
{
cout <<"("<< ptr->data1<<","<< ptr->data2<<","<< ptr->data3<<")"<<endl;
}
}
}

int main()
{
ifstream infile("test2.txt");
ofstream outfile("final3.txt");
int numofblock;
string none;
infile>>numofblock;
getline (infile,none);
LinkedList list;
LinkedList::NodePointer ptr = list.head();
for(int i=0;i<numofblock;i++)
{
char non;
ElementType d1,d2,d3;
infile>>non>>d1>>non>>d2>>non>>d3;
getline (infile,none);
list.insert(d1,d2,d3,ptr);
ptr = ptr->next;
}
LinkedList::NodePointer tem = list.head();
for(int i=0;i<numofblock-1;i++)
{
list.compare(tem);
tem = tem->next;
}
list.compare(tem);
list.display();
if(numofblock ==1)
{
list.display();
}
LinkedList::NodePointer ptr2 = list.first;
while (ptr2 ->next != list.tailptr)
{
ptr2 = ptr2 ->next;
LinkedList::NodePointer tem = ptr2->next;
if(tem->data1 == ptr2->data1)
{
continue;
}
else
{
outfile <<"("<< ptr2->data1<<","<< ptr2->data2<<","<< ptr2->data3<<")"<<endl;
}
}
if(numofblock ==1)
{
LinkedList::NodePointer ptr3 = list.first;
while (ptr3 ->next != list.tailptr)
{
ptr3 = ptr3 ->next;
LinkedList::NodePointer tem = ptr3->next;
if(tem->data1 == ptr3->data1)
{
continue;
}
else
{
outfile <<"("<< ptr3->data1<<","<< ptr3->data2<<","<< ptr3->data3<<")"<<endl;
}
}
}
return 0;
}

2013/10/31 23:12
應用擴展 工具箱
回覆: 請教有關segmentation fault...
管理員
註冊日期:
2012/1/14 18:41
所屬群組:
討論區管理群
等級: 20
HP : 0 / 480
MP : 159 / 10441
EXP: 23
離線
錯誤點
at test.cpp:86
86 if((origin->data2)-(ptr->data1)>0)

ptr 是NULL

還有,他建議你
class LinkedList
這個要有contructor 因為你的class裏面有pointer。

一些建議:
NodePointer ptr = origin->next;
if((origin->data2)-(ptr->data1)>0)
這類型的code請再中間插上判斷是不是null,不然你很容易就會出現null。
不使用的pointer也請指定NULL。

2013/11/1 8:17
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.