ostringstream 和复制构造函数的问题
可能的重复:
为什么不允许复制 stringstream?
如何在 C++ 中从一个 stringstream 对象复制到另一个 stringstream 对象?< /a>
编译类 T 失败,Visual C++ 和 GCC 产生 iostreams 模板错误。下面是代码:
#include <sstream>
class T
{
static T copy;
std::ostringstream log;
T() {}
T(const T& t) {log = t.log;}
~T() {copy = *this;}
};
T T::copy;
将 log 数据成员类型更改为字符串使其编译并运行正常。这是合法行为吗?
Possible Duplicates:
Why copying stringstream is not allowed?
how copy from one stringstream object to another in C++?
Compiling class T fails with Visual C++ and GCC producing iostreams template errors. Here is the code:
#include <sstream>
class T
{
static T copy;
std::ostringstream log;
T() {}
T(const T& t) {log = t.log;}
~T() {copy = *this;}
};
T T::copy;
Changing log data member type to string makes it compile and run OK. Is this a legitimate behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++ 中任何流类的复制构造函数和复制赋值已设为
私有
。这意味着,您无法复制std::ostringstream
对象:Copy constructor and copy-assignment of any stream class in C++ has been made
private
. That means, you cannot make copy ofstd::ostringstream
object:std::ostringstream
不可复制,这就是您收到错误的原因。请参阅此答案了解更多详细信息如何克服这个问题。std::ostringstream
is not copyable that's why you are getting error. See this answer for more details to know how you can overcome this problem.我认为 ostringstream 没有重载的赋值(=)运算符。
I think ostringstream has no overloaded assignment(=) operator.