1
我想在使用初始值設定項列表的構造函數中初始化一個私人std::ofstream
(例如,在main
中打開的文件)。我有以下代碼:使用初始值設定項列表在類中初始化std :: ofstream
class MyClass{
std::ofstream ofs;
public:
MyClass(const std::ofstream &ofs): ofs(ofs) { }
};
,我得到以下編譯錯誤:
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>
]
這裏發生了什麼?
啊,我忘了它試圖複製構建。爲了實現我的目的,我只需要聲明作爲參考。謝謝! – Arcinde
@ user49164 yup參考將工作,很高興我可以幫助 –