I want to initialize a private std::ofstream
(say, to a file opened in main
) in a constructor using an initializer list. 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?