我有一个像这样在我的类中声明的 fstream 对象(只是一个例子):
class Asd {
public:
Asd();
private:
std::fstream stream;
};
现在,当调用构造函数时,我想像这样指定 fstream 参数
Asd::Asd() {
this->stream = std::fstream(file, std::fstream::in);
}
然后在我拥有的所有类函数中使用该流,但它不起作用。VS 给我的一个错误是:
no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
所以我阅读了这一点,我能找到的只是我不能(或者更确切地说:不应该)复制一个流,实际上我什至不想这样做。有人说可以将其添加到构造函数中:
Asd::Asd() : stream(file, std::fstream::in) {
...
}
但它打印出同样的错误,我不知道该怎么办......还有其他人说我必须引用该对象但我不知道如何?我只是想让这个工作,但我想不通:(
编辑:这是完整的错误信息
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]