如何使用 wstringstream 类变量?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为编译器隐式声明了类
foo
的复制构造函数。std::wstringstream
是不可复制的,因为它继承自ios_base
。将您的类更改为:
编译器应该指出罪魁祸首。
This is because the compiler is implicitly declaring a copy constructor for class
foo
.std::wstringstream
is noncopyable, because it inherits fromios_base
.Change your class to this:
and the compiler should point you at the culprit.
假设
externalfunction
行上缺少;
是一个拼写错误,我无法获得确切的错误消息,但看起来可能是externalfunction
需要一个std::string
作为其参数。事实上,mBufferStream.str()
提供了一个不能隐式转换的std::wstring
。Assuming the missing
;
on theexternalfunction
line is a typo, I wasn't able to get your exact error message, but it looks like perhapsexternalfunction
expects astd::string
as its parameter. In factmBufferStream.str()
provides astd::wstring
which can't be implicitly converted.