如何在C++ msgpack中打包字符串?

问题描述 投票:0回答:1
msgpack::object elem;
std::string tempString = "string";
elem["type"] = msgpack::pack(tempString);

以上代码不起作用

error: no matching function for call to 'pack(std::string&)'

如何将字符串打包到 msgpack::object 中?

c++ msgpack
1个回答
0
投票

msgpack::pack()
是一个静态成员函数,它显然是无状态的,需要一个将
tempString
打包到的目标。目标可以是任何具有成员函数
write(const char*, std::size_t);
的类。例如,使用
std::stringstream
:

msgpack::object elem;
std::string tempString = "string";
std::stringstream messagePacked;
elem["type"] = msgpack::pack(messagePacked, tempString);
© www.soinside.com 2019 - 2024. All rights reserved.