RapidJson中的kArrayType与String

3

我有以下代码,但它无法编译。 我无法想出原因,请帮忙。

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);

我遇到了以下错误:
错误:没有与调用‘rapidjson::GenericValue> ::PushBack(const char*,rapidjson::GenericDocument> ::AllocatorType&)’匹配的函数 messageArr.PushBack(test.c_str(),allocator);

完成 - RapidJson具有不同类型的字符串值:已分配(在构建时需要长度),简单的const char *包装器(如果超出范围将会崩溃)和/或短字符串* 15个字符或更少 - 或类似于此。由于您想要使用分配器,我假设您想要通过复制StrValue - 答案显示了如何实现。 - Adrian Colomitchi
2个回答

6

[编辑] - 解决方案:

  std::string test = std::string("TEST");
  rapidjson::Value strVal;
  strVal.SetString(test.c_str(), test.length(), allocator);
  messageArr.PushBack(strVal, allocator);

请参阅RapidJson教程 - 创建字符串

流畅式:

 messageArr.PushBack(
      rapidjson::Value{}.SetString(test.c_str(), test.length(), allocator),
      allocator
  );

2
我尝试了,但没有成功,然后出现了以下错误: 来自此处所需 /usr/include/rapidjson/document.h:1342:29: 错误:没有找到与‘rapidjson::GenericValue<rapidjson::UTF8<> >::GenericValue(std::__cxx11::basic_string<char>&)’相匹配的函数 GenericValue v(value); ^ - nilan
你有成功编译过吗?当我尝试像rapidjson教程中讲解copyFrom那样pushback我的rapidjson::Value &时,我也遇到了同样的错误。 - Michele

1
using namespace rapidjson;
using namespace std;

Value array(kArrayType);
string test = "TEST";
Value cat(test.c_str(), allocator);
array.PushBack(cat, allocator);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接