1

可能的重复:
为什么不允许复制字符串流?
如何在 C++ 中从一个字符串流对象复制到另一个?

编译类 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;

将日志数据成员类型更改为字符串使其编译和运行正常。这是合法的行为吗?

4

3 回答 3

4

C++ 中任何流类的复制构造函数和复制赋值已经完成private。这意味着,您不能复制std::ostringstream对象:

std::ostringstream ss;

std::ostringstream ss1(ss); //not allowed - copy-constructor is private
ss1=ss; //not allowed - copy-assignment is private
于 2011-08-13T10:06:00.663 回答
3

std::ostringstream不可复制,这就是您收到错误的原因。有关更多详细信息,请参阅此答案以了解如何克服此问题。

T(const T& t)  {log << t.log.rdbuf(); }
于 2011-08-13T10:06:30.297 回答
1

我认为ostringstream没有重载的赋值(=)运算符。

于 2011-08-13T10:05:36.117 回答