2012-02-03 3 views
1

이 질문으로 많은 것을 검색해 보았지만 비슷한 질문을 많이 발견했지만 해결 방법을 찾지 못했습니다.C++ ifstream 클래스 오류

컴파일시
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 것을 수집 한

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 (); 
}; 

과 해당 기능 : 나는 클래스를 선언하고 있습니다 과제와 모든면에서 다소 특별하지만, 수업에 포함시키는 방법을 모릅니다. 도와 주셔서 미리 감사드립니다.

편집 : 나는 같은 일반 변수를 사용하는 것과 위의 클래스의 여러 순열, 시도했다 :

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의 값은 유효한 값을 가져야합니다. 참조는 항상 유효한 값을 가져야합니다.

이니셜 라이저 목록은 항상 생성자 본문보다 우선해야합니다.