1

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?

4

1 回答 1

3

看看cppreference

basic_ofstream( const basic_ofstream& rhs) = delete;    (5)     (since C++11)

无法复制构造 a basic_ofstream

可能您使用的是 c++11 之前的编译器,它private在库中被标记为。

于 2013-08-20T01:01:14.817 回答