msgpack::object elem;
std::string tempString = "string";
elem["type"] = msgpack::pack(tempString);
以上代码不起作用
error: no matching function for call to 'pack(std::string&)'
如何将字符串打包到 msgpack::object 中?
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);