2012-02-03 3 views
1

私はこの質問で多くを検索し、同様の質問をたくさん見つけましたが、解決策を見つけることができませんでした。私は、クラスを宣言しています:クラスエラーでif ++ ifstream

class File { 

    public: 
      string fileName; 
      std::ifstream & flinstream; 
      Password pass; 
      //Next block to look at 
      unsigned int nb; 
      unsigned int sectorsLeft; 
File (string name,string passd); 
File (); 
}; 

と対応する関数:

File::File (string name,string passd) { 
     fileName = name; 
     const char* cstr = name.c_str(); 
     pass = Password(passd); 
     flinstream = std::ifstream(cstr); 
     if(!flinstream.good()) { 
      string err = "The file '"; 
      err.append(name); 
      err.append("' could not be opened!"); 
      callError(err,3); 
     } 
    } 

コンパイル時に、私は次のエラーを取得:

[0] => out.cpp: In constructor ‘File::File(std::string, std::string)’: 
    [1] => out.cpp:130:3: error: uninitialized reference member ‘File::flinstream’ 
    [2] => In file included from /usr/include/c++/4.5/ios:39:0, 
    [3] =>     from /usr/include/c++/4.5/ostream:40, 
    [4] =>     from /usr/include/c++/4.5/iostream:40, 
    [5] =>     from out.cpp:1: 
    [6] => /usr/include/c++/4.5/bits/ios_base.h: In member function ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’: 
    [7] => /usr/include/c++/4.5/bits/ios_base.h:788:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private 
    [8] => /usr/include/c++/4.5/iosfwd:77:11: error: within this context 
    [9] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’: 
    [10] => /usr/include/c++/4.5/iosfwd:83:11: note: synthesized method ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ first required here 
    [11] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’: 
    [12] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’ first required here 
    [13] => /usr/include/c++/4.5/streambuf: In member function ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’: 
    [14] => /usr/include/c++/4.5/streambuf:781:7: error: ‘std::basic_streambuf<_CharT, _Traits>::__streambuf_type& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private 
    [15] => /usr/include/c++/4.5/iosfwd:108:11: error: within this context 
    [16] => /usr/include/c++/4.5/iosfwd: In member function ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’: 
    [17] => /usr/include/c++/4.5/iosfwd:111:11: note: synthesized method ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’ first required here 
    [18] => out.cpp: In constructor ‘File::File(std::string, std::string)’: 
    [19] => out.cpp:134:36: note: synthesized method ‘std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)’ first required here 
    [20] => out.cpp: In constructor ‘File::File()’: 
    [21] => out.cpp:142:3: error: uninitialized reference member ‘File::flinstream’ 
    [22] => out.cpp: In member function ‘File& File::operator=(const File&)’: 
    [23] => out.cpp:51:12: error: non-static reference member ‘std::ifstream& File::flinstream’, can't use default assignment operator 
    [24] => out.cpp: In function ‘int main(int, char**)’: 
    [25] => out.cpp:166:57: note: synthesized method ‘File& File::operator=(const File&)’ first required here 
) 

私はifstreamことを集めました割り当てとすべてのことでかなり特定ですが、私はどのようにクラスにそれを含めるか分かりません。あなたの助けを前もってありがとう!

編集:私はこのような通常の変数を使用して、上記クラスのいくつかの並べ替えを試みた:

std::ifstream flinstream; 

と同様にopen()関数を使用が提案:

flinstream.open(cstr); 

をしかし、エラーは同じままです。手始めに

答えて

3

、あなたが本当にifstreamから参照を場合を除き、私はちょうど、割り当てC++ 03(C++の以前のバージョン)では

std::ifstream flinstream; 

としてあなたのクラスでifstreamを宣言しますストリームクラスでは無効になっているので、行は

flinstream = std::ifstream(cstr); 

コンパイルされません。ただし、std::ifstream::openメソッドを使用して、これを行うことができます。

flinstream.open(cstr); 
/* ... remaining processing ... */ 

+0

私もこれを試してみました。役に立たなかった – Precursor

+0

@ Precursor- Ah!私は何が起こっているのを見る。クラスのフィールドとして 'ifstream'があるので、' ifstream'のコピーサポートがないので、あなたのクラスのインスタンスをコピーまたは割り当てることはできません。フィールドとして 'ifstream'が本当に必要ですか?もしそうなら、本当にあなたのクラスをコピーする必要がありますか? – templatetypedef

+0

これはかなり必須です。私のコンパイラで控えめにコピーする方法はありますか?また、クラスを補うためにifstreamsのセカンダリ配列を作成する必要があります(クラス内にないので、同じサイズの配列になっています)。 – Precursor

0

参照を未初期化のままにすることはできません。初期化子リスト内のすべての参照メンバを初期化する必要があります。

flinstreamがコンストラクタの本体で初期化されています。コンストラクタの本体が実行されるときに、flinstreamの値は正当な値でなければなりません。参照は常に正当な価値を持っていなければなりません。

初期化子リストは、常にコンストラクタ本体よりも優先されるべきです。