nil*_*lan 3 c++ stdstring rapidjson
我有以下代码,但无法编译。我想不出原因,请帮助。
rapidjson::Document jsonDoc;
jsonDoc.SetObject();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
rapidjson::Value messageArr(rapidjson::kArrayType);
std::string test = std::string("TEST");
messageArr.PushBack(test.c_str(), allocator);
Run Code Online (Sandbox Code Playgroud)
给我以下错误;
错误:没有匹配的函数调用 'rapidjson::GenericValue >::PushBack(const char*, Rapidjson::GenericDocument >::AllocatorType&)'
messageArr.PushBack(test.c_str(), allocator);
[编辑] - 解决方案:
std::string test = std::string("TEST");
rapidjson::Value strVal;
strVal.SetString(test.c_str(), test.length(), allocator);
messageArr.PushBack(strVal, allocator);
Run Code Online (Sandbox Code Playgroud)
流利风格:
messageArr.PushBack(
rapidjson::Value{}.SetString(test.c_str(), test.length(), allocator),
allocator
);
Run Code Online (Sandbox Code Playgroud)