无法访问类“std::basic_ios<_elem ,_traits>”中声明的私有成员

发布于 2024-11-30 17:04:16 字数 620 浏览 1 评论 0原文

此特定方法有问题,不知道如何解决!我收到的错误是上面的:

“错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2024-12-07 17:04:16

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

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

当您按(而不是引用)返回时,则需要复制流对象,但在C++中复制任何流对象已经是通过将复制构造函数1设为私有禁用

1.以及复制分配。

要了解为什么复制任何流已被禁用,请在此处阅读我的详细答案:

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;
}

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:

怕倦 2024-12-07 17:04:16

您不能复制流,而是返回引用,更改为

ostream& operator<<( ostream & stream, ProcessClass const & rhs )

You cannot copy streams, instead return a reference, change to

ostream& operator<<( ostream & stream, ProcessClass const & rhs )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文