【发布时间】:2020-02-11 04:51:40
【问题描述】:
我正在编写一个从套接字向终端写入数据的类。为了测试我的一些方法,我为我的一个类变量设置了一个 setter 方法,它恰好是一个 std::stringbuf 对象。尝试将新对象分配给类实例时,我的编辑器在分配中显示错误。为清楚起见,这是我的类原型:
class MainServer : BaseServer
{
public:
MainServer();
void setByteBuffer(std::stringbuf);
private:
int valread;
std::stringbuf byteBuffer;
void dataRead();
void terminalWrite();
};
给喵喵麻烦的方法是setByteBuffer,实现是:
void MainServer::setByteBuffer(std::stringbuf testByteBuffer)
{
byteBuffer = testByteBuffer;
}
我的编辑给我的错误是:
std::stringbuf &std::stringbuf::operator=(const std::stringbuf &)
+1 overload
@brief %Vector assignment operator.
@param __x A %vector of identical element and allocator types.
All the elements of @a __x are copied, but any unused capacity in
@a __x will not be copied.
Whether the allocator is copied depends on the allocator traits.
function "std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc> &) [with _CharT=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" (declared at line 140 of "/usr/include/c++/9/sstream") cannot be referenced -- it is a deleted function
似乎stringbuf 类的赋值(意思是复制)构造函数不适合我正在尝试做的事情。有什么方法可以解决这个问题?我认为一种解决方案是使用stringbuf *,但这似乎是个坏主意,因为无论如何这只会用于测试目的。
【问题讨论】:
-
std::stringbuf对象不可复制。也没有“赋值构造函数”之类的东西。 O 猜测解决方案是使用其他东西而不是stringbuf。 -
糟糕,我的意思是写复制构造函数。那么还有什么可以达到这个目的呢?
-
好吧,从这个问题来看,您使用
stringbuf的目的是什么。 -
我使用
stringbuf来存储来自套接字的数据。也许另一种选择是使用vector<char>对象。关键是它需要能够容纳无限量的数据,所以数组不是一个好的选择。 -
那你肯定应该从一开始就使用
vector。
标签: c++ variable-assignment copying