我有一个运算符重载的类,如下所示:
friend ostream operator<< (ostream &stream, const Item &item){
stream << item.title << " - " << item.format << " (Loaned to " << item.name << ") on " << item.date;
return stream;
}
我的程序无法编译并给出以下错误:
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class '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 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
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>::basic_ostream(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
发生了什么事?为什么说私人会员无法访问,我很困惑。
答案 0 :(得分:6)
您必须返回参考。
friend ostream& operator<< (ostream &stream, const Item &item)
// ^
stream的copy-constructor被删除/私有。
答案 1 :(得分:0)
问题在于:
friend ostream operator<< (ostream &stream, const Item &item){
您需要通过引用返回流,ostream对象不可复制。