2013-08-03 48 views
0

這是我的第一篇文章。右大括號處出現分段錯誤

我試着記住說明,但如果我錯過了某些東西,我會提前道歉。

該計劃是一個班級的任務,我們要使用三個不同的鏈表(學生,課程和交易,因爲想要更好的單詞)而製作成績冊。

我張貼將通過列表中搜索塊(假設有一個,尋找具有相同的信息現有對象,那就是csearch函數做什麼。

如果它沒有找到一個,然後創建一個新的對象,並着手把它在列表中,總是在尾部。

所有這一切工作,直到它到達一個函數的右括號,然後段故障。

我花了一天的好時間試圖弄清楚爲什麼會發生這種情況,我希望喲你可以幫助我。

void classes::addcourse(classes section) 
    { 
    int courseid; 
    char title[81]; 
    course *temp= NULL, *search=NULL; 

    cout<<"Please enter the course id number."<<endl; 
    cin>>courseid;getchar(); 

    cout<<"Please enter a course name."<<endl; 
    cin>>title; 

    search=section.csearch(courseid);    
    if (search != NULL) 
     {             
     cout<<"This course has already been entered."<<endl; 
     return;            
     } 

    temp=new course(courseid); 
    if (head==NULL) 
     { 
     sethead(temp); 
     settail(temp); 
     } 
    tail->setnext(temp); 
    settail(temp); 
    section.setcnum(section.getcnum()+1); 
    cout<<section.getcnum()<<endl; 
    temp->setname(title); 
    } 

如果有幫助,這裏是對象和容器的類定義,它們分別存儲在.h文件中。

class course { 
    private: 
    int cid; 
    int average; 
    course *next; 
    char name[81]; 

    public: 
    course(int); 
    void saveyourself(FILE *write); 
    void loadyourself(FILE *read); 

    //accessor functions 
    int getcid() {return cid;} 
    int getaverage() {return average;} 
    course* getnext() {return next;} 

    //mutator functions 
    void setnext(course *val) {next=val;} 
    void getname() {cout<<name;} 
    void setcid (int newcid) {cid=newcid;} 
    void setaverage (int newav) {average=newav;} 
    void setname (char word[]) {strcpy(name, word);} 
}; 

class classes { 
    private: 
    course *head; 
    course *tail; 
    int numcourse; 

    public: 
    classes(); 
    ~classes(); 
    course* csearch (int course); 
    void addcourse(classes section); 
    void classaverage(enrollment semester, classes section); 
    void save(); 
    void load(); 

    //accessor functions 
    int getcnum() {return numcourse;} 
    course* gethead() {return head;} 
    course* gettail() {return tail;} 

    //mutator functions 
    void setcnum(int num) {numcourse=num;} 
    void sethead(course *val) {head=val;} 
    void settail(course *val) {tail=val;} 

}; 

如果它出現時,我使用的字符數組的字符串,因爲這是我在當我厭倦了來回切換終於塵埃落定。

編輯:對不起不添加構造函數,我忘了他們,在這裏,他們是:

course::course(int id=-1) 
    { 
    cid=id; 
    average=0; 
    strcpy(name, "name"); 
    next=NULL; 
    } 


classes::classes() 
    { 
    numcourse=0; 
    head=NULL; 
    tail=NULL; 
    } 

我一直偏執未初始化字符串,所以這就是爲什麼它已經設置的東西。我通常將它們初始化爲「\ 0」,但這使我擔心文件I/O。

+0

您是否嘗試過使用Valgrind? –

+0

你的問題幾乎肯定在於你沒有遵循[The Rule of 3](http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29)。 –

+1

我可以提出兩種可能性。 (1)右大括號爲您在塊中製作的對象運行dtors。所以一個Dtor可能會導致崩潰。 (2)您的數組標題[81]可能溢出並丟棄堆棧。 –

回答

0

我不相信這是做你想要什麼:

cin>>title; 

如果你打算使用C風格的字符數組,你可能想使用cin.getline()像這樣:

cin.getline(title, 81); 

這將確保您不會溢出title[],並且它會在最後放置一個NUL終止符。它也會在最後吃掉換行符,而不是放在title[]

更多的信息在這裏:http://www.cplusplus.com/reference/istream/istream/getline/?kw=istream%3A%3Agetline

+0

getline是我反覆使用的東西,結果沒有明顯的差異。我沒有使用任何比Visual C++ Express中包含的調試工具更復雜的調試工具,並且提供了關於名稱的0x0000000c 的消息,但我不確定指向哪條線。 – wolfman2001

+0

你在哪裏初始化'head'和'tail'。我沒有在你的構造函數中看到代碼這樣做,但是你的構造函數在上面的代碼中不可見。 –