[英]Initialize a std::ofstream in a class using initializer list
I want to initialize a private std::ofstream
(say, to a file opened in main
) in a constructor using an initializer list. 我想使用初始化列表在构造函数中初始化私有
std::ofstream
(例如,在main
中打开的文件)。 I have the following code: 我有以下代码:
class MyClass{
std::ofstream ofs;
public:
MyClass(const std::ofstream &ofs): ofs(ofs) { }
};
and I get the following compile error: 我得到以下编译错误:
error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files (x86)\microsoft visual studio 11.0\vc\include\fstream(1034) : see declaration of 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
What's happening here? 这里发生了什么事?
Take a look at cppreference 看看cppreference
basic_ofstream( const basic_ofstream& rhs) = delete; (5) (since C++11)
It is not possible to copy construct a basic_ofstream
. 复制构造
basic_ofstream
是basic_ofstream
。
Possibly you are using pre-c++11 compiler and it is marked as private
in the library. 可能您使用的是pre-c ++ 11编译器,它在库中标记为
private
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.