无法访问在'std :: basic_ios< _Elem,_Traits>'中声明的私有成员 [英] Cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

查看:348
本文介绍了无法访问在'std :: basic_ios< _Elem,_Traits>'中声明的私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个特定方法有问题,不知道如何解决!我得到的错误是上述:

Having an issue with this particular method and not sure how to resolve it! The error I'm getting is the above:


错误C2248:'std :: basic_ios< _Elem,_Traits> :: basic_ios' :can not
访问在类
中声明的私有成员std :: basic_ios< _Elem,_Traits>'C:\Program Files \Microsoft Visual
Studio 10.0 \VC\include\\ \\ strream 604

"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"

我的方法是:

ostream operator<<( ostream & stream, ProcessClass const & rhs )
{
  stream << rhs.name_;
  return stream;
}

在标题中:

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!

推荐答案

返回类型应为 ostream& ,它是对 ostream 的引用。

The return type should be ostream & which is a reference to ostream.

ostream & operator<<( ostream & stream, ProcessClass const & rhs )
{    //^^^ note this!
  stream << rhs.name_;
  return stream;
}

em> reference ),那么这需要复制流对象,但是通过使复制构造函数 sup> 1

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

要知道为什么已停用任何流的复制,请在此处阅读我的详细答案:

To know why copying of any stream has been disabled, read my detail answer here:

  • Why copying stringstream is not allowed?

这篇关于无法访问在'std :: basic_ios&lt; _Elem,_Traits&gt;'中声明的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆