3

I have following code but its cannot compiled. I cannot think about a reason, please hlep.

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

Giving me following error;

error: no matching function for call to ‘rapidjson::GenericValue >::PushBack(const char*, rapidjson::GenericDocument >::AllocatorType&)’
messageArr.PushBack(test.c_str(), allocator);

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
nilan
  • 61
  • 1
  • 6
  • Done - RapidJosn has different sorts of String Values: allocated (will need a length when constructing), simple `const char*` wrappers (which will blow if going out of scope) and/or `short strings` *15 chars or less - or somethng like that). Since you wanted with allocators, I assumed you wanted a by-copy StrValue - the answer shows how. – Adrian Colomitchi Sep 22 '16 at 07:12

2 Answers2

6

[Edited] - Solution:

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

See RapidJson tutorial - Create String

Fluent-style:

 messageArr.PushBack(
      rapidjson::Value{}.SetString(test.c_str(), test.length(), allocator),
      allocator
  );
Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23
  • 2
    I tried, but no luck, then giving the following error, equired from here /usr/include/rapidjson/document.h:1342:29: error: no matching function for call to ‘rapidjson::GenericValue >::GenericValue(std::__cxx11::basic_string&)’ GenericValue v(value); ^ – nilan Sep 22 '16 at 06:28
  • Did you ever get this to compile? I get the same error when I try to pushback my rapidjson::Value & like he has on the rapidjson tutorial when they talk about copyFrom. – Michele Feb 27 '17 at 16:26
1
using namespace rapidjson;
using namespace std;

Value array(kArrayType);
string test = "TEST";
Value cat(test.c_str(), allocator);
array.PushBack(cat, allocator);
Devolus
  • 21,661
  • 13
  • 66
  • 113