2012-06-03 36 views
1

賽格故障main()我頭:的iostream,fstream時了iomanip,MyClass.h虛擬的IStream和功能return語句

char temp; 
MyClass *class1; 
ifstream inf("file") 
while(inf >> temp) 
{ 
    if(temp == 'A') class1 = new Myclass(); 
    inf >> *class1; 
} 

在Myclass.h:

class MyClass{ 
    int num; 
protected: 
    virtual istream& read(istream &is) 
    { /* char temp; is >> temp >> num; */ 
    return is; //seg faults on this line I believe 
    } 
public: 
    friend istream& operator >> (istream &is, MyClass &class1) {return class1.read(is);} 
}; 

計劃應該讀取文件,看看它是否以'a'開頭,然後創建一個類,並解析信息。然後MyClass將讀取函數傳給istream,這就是我有seg錯誤的地方。程序段錯誤就像進入while循環一樣。如果我刪除了虛擬閱讀功能,它不會出現分段錯誤。 gdb只是指向0x000000001?

這是一個家庭作業問題,我需要爲read()(和其他)函數寫體。

+0

當我從函數聲明中刪除'虛擬'時,一切都像一個魅力!但我不能這樣做,但... – user1078719

回答

0

現在問題就解決了!暫時我在聲明中沒有使用「虛擬」功能。當我從MyClass派生子類時,我把「虛擬」放回去,一切正常!