C++ C++;类中的ifstream错误
我搜索了很多关于这个问题的地方,发现了很多类似的问题,但我没有找到解决方案。我正在声明一个类:C++ C++;类中的ifstream错误,c++,ifstream,C++,Ifstream,我搜索了很多关于这个问题的地方,发现了很多类似的问题,但我没有找到解决方案。我正在声明一个类: class File { public: string fileName; std::ifstream & flinstream; Password pass; //Next block to look at unsigned int nb;
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
)
以及使用建议的open()
函数:
flinstream.open(cstr);
但是,错误仍然是一样的。对于初学者来说,除非您真的想要引用
ifstream
,否则我将在您的类中将ifstream
声明为
std::ifstream flinstream;
在C++03(C++的早期版本)中,流类的赋值被禁用,因此
flinstream = std::ifstream(cstr);
不会编译。但是,您可以使用std::ifstream::open
方法执行以下操作:
flinstream.open(cstr);
/* ... remaining processing ... */
希望这有帮助 引用不能保持未初始化状态。必须初始化初始值设定项列表中的所有引用成员 正在构造函数体中初始化flinstream。在构造函数主体执行时,flinstream的值必须具有合法值。引用必须始终具有法律价值
初始值设定项列表应始终优先于构造函数主体。@preducer-Ah!我知道发生了什么事。由于类的字段是
ifstream
,因此无法复制或分配类的实例,因为ifstream
不支持复制。您真的需要ifstream
作为字段吗?如果是这样,你真的需要能够复制你的类吗?这是非常必要的。有没有办法让我的编译器不情愿地复制它,或者我应该制作一个ifstreams的二级数组来补充类(因此它们不在类中,只是在一个大小相同的数组中)?@preducer-我会首先找出你在哪里制作副本,然后看看是否有必要。如果是,那么您应该定义自己的复制构造函数和赋值运算符,以便每次都可以手动重新打开该文件。@preducer:您可以使用std::ifstream*
并使用智能指针类,也可以自己管理内存分配。可以毫无问题地复制指针。@templatetypedef,我只是初始化了一个充满File类的大型全局数组,到目前为止我已经完成了所有的工作。
flinstream.open(cstr);
/* ... remaining processing ... */