无法访问类“std::basic_ios<_elem ,_traits>”中声明的私有成员
此特定方法有问题,不知道如何解决!我收到的错误是上面的:
“错误 C2248:'std::basic_ios<_Elem,_Traits>::basic_ios':不能 访问类中声明的私有成员 'std::basic_ios<_Elem,_Traits>' C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream 604"
我的方法是:
ostream operator<<( ostream & stream, ProcessClass const & rhs )
{
stream << rhs.name_;
return stream;
}
在标题中:
friend std::ostream operator<<( std::ostream & stream, ProcessClass const & rhs );
关于如何解决这个问题的任何想法?我认为这与通过引用而不是值传递有关...但我有点使困惑!
Having an issue with this particular method and not sure how to resolve it! The error I'm getting is the above:
"error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot
access private member declared in class
'std::basic_ios<_Elem,_Traits>' C:\Program Files\Microsoft Visual
Studio 10.0\VC\include\ostream 604"
My method is:
ostream operator<<( ostream & stream, ProcessClass const & rhs )
{
stream << rhs.name_;
return stream;
}
And in the header:
friend std::ostream operator<<( std::ostream & stream, ProcessClass const & rhs );
Any ideas on how to resolve this? I think it is something to do with passing by reference instead of value... but I'm a bit confused!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回类型应为
ostream &
,它是对ostream
的引用。当您按值(而不是引用)返回时,则需要复制流对象,但在C++中复制任何流对象已经是通过将复制构造函数1设为
私有
来禁用。1.以及复制分配。
要了解为什么复制任何流已被禁用,请在此处阅读我的详细答案:
The return type should be
ostream &
which is a reference toostream
.When you return by value (instead of reference), then that requires copying of stream object, but copying of any stream object in C++ has been disabled by having made the copy-constructor1
private
.1. and copy-assignment as well.
To know why copying of any stream has been disabled, read my detail answer here:
您不能复制流,而是返回引用,更改为
You cannot copy streams, instead return a reference, change to