1

ヘッダーがあります:iostream、fstream、 iomanip main()、MyClass.h

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を読み取り関数に渡します。ここでセグメンテーション違反が発生します。whileループに入るときと同じように、セグメンテーション違反をプログラムします。仮想読み取り機能を削除しても、セグメンテーション違反は発生しません。gdbは??で0x000000001を指しているだけです

これは宿題の問題であり、read()(およびその他の)関数の本文を作成する必要があります。

4

1 に答える 1

0

問題は今解決しました!とりあえず、宣言に「virtual」を付けずにこれらの関数を操作しました。MyClassから子クラスを派生させたとき、「仮想」を元に戻し、すべてが正常に機能しました。

于 2012-06-03T23:09:35.427 に答える