Rapidjson: AddMember can`t set string as key

Created on 13 Mar 2015  ·  2Comments  ·  Source: Tencent/rapidjson

string key="hello";
Value v;
json_temp.AddMember(key.c_str(), v, json_temp.GetAllocator());  //compile error

json_temp.AddMember("hello", v, json_temp.GetAllocator());  //compile OK

how could I solve this problem?

Most helpful comment

You need to construct a temporary Value, creating a _copy_ of the string in case of a dynamic name:

Value n(key.c_str(), json_temp.GetAllocator());
json_temp.AddMember(n, v);

See also the example in the tutorial.

All 2 comments

You need to construct a temporary Value, creating a _copy_ of the string in case of a dynamic name:

Value n(key.c_str(), json_temp.GetAllocator());
json_temp.AddMember(n, v);

See also the example in the tutorial.

And what sense does it make that you have to create a temporary?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vzaluckis picture vzaluckis  ·  4Comments

ywsswy picture ywsswy  ·  3Comments

accelerated picture accelerated  ·  6Comments

leeyiw picture leeyiw  ·  4Comments

lasalvavida picture lasalvavida  ·  5Comments