如何使用 wstringstream 类变量?

发布于 2024-09-10 12:03:11 字数 825 浏览 9 评论 0原文

我有一个 std::wstringstream ,我在类中将其用作缓冲区,并且该类中的大部分方法都使用它。但是,当我尝试执行以下操作时:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
};

我收到以下错误:

错误 C2248:“std::basic_ios<_Elem,_Traits>::basic_ios”:无法访问类“std::basic_ios<_Elem,_Traits>”中声明的私有成员

这显然不是我的确切课程,但设置相同。关于我可能做错了什么有什么想法吗?我正在使用 Microsoft Visual Studio 2005。

[编辑]显示 .cpp 文件中方法体的使用(作为其使用的示例):

void foo::methodA(int x, int y)
{
  mBufferStream << "From " << x << " To " << y;
  externalfunction(mBufferStream.str());  // Prints to message service
  mBufferStream.str(L"");
}

I have a std::wstringstream that I'm using as sort of a buffer in my class and it is used by a good portion of the methods in this class. However, when I try to do something like this:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
};

I get the following error:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

This isn't my exact class obviously, but it is the same setup. Any thoughts as to what I may be doing wrong? I am using Microsoft Visual Studio 2005.

[Edit] showing use in method body in .cpp file (as an example of it's use):

void foo::methodA(int x, int y)
{
  mBufferStream << "From " << x << " To " << y;
  externalfunction(mBufferStream.str());  // Prints to message service
  mBufferStream.str(L"");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情深如许 2024-09-17 12:03:11

这是因为编译器隐式声明了类 foo 的复制构造函数。 std::wstringstream 是不可复制的,因为它继承自 ios_base

将您的类更改为:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
    foo(const foo&); //noncopyable
    void operator=(const foo&)
};

编译器应该指出罪魁祸首。

This is because the compiler is implicitly declaring a copy constructor for class foo. std::wstringstream is noncopyable, because it inherits from ios_base.

Change your class to this:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
    foo(const foo&); //noncopyable
    void operator=(const foo&)
};

and the compiler should point you at the culprit.

把梦留给海 2024-09-17 12:03:11

假设 externalfunction 行上缺少 ; 是一个拼写错误,我无法获得确切的错误消息,但看起来可能是 externalfunction 需要一个 std::string 作为其参数。事实上,mBufferStream.str()提供了一个不能隐式转换的std::wstring

Assuming the missing ; on the externalfunction line is a typo, I wasn't able to get your exact error message, but it looks like perhaps externalfunction expects a std::string as its parameter. In fact mBufferStream.str() provides a std::wstring which can't be implicitly converted.

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