2

I am all new to C++ and am running into an issue. I am using rapidJSON to create JSON documents.

void setKeyValue() {
        Value obj(kObjectType);
        Value key("key");
        Value val(42);;
        obj.AddMember(key, val, d.GetAllocator());
    }

Works as expected. But when I try to replace the call to key to make it use a passed in param, like so:

void setKeyValue(string myKey) {
        Value obj(kObjectType);
        Value key(myKey);
        Value val(42);;
        obj.AddMember(key, val, d.GetAllocator());
    }

The myKey in Value key(myKey) get a red curly underling in Visual Studio saying the following:

enter image description here

What is causing this and how can I solve it?

acraig5075
  • 10,588
  • 3
  • 31
  • 50
SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
  • did you check if there is a constructor for `Value` that takes a `std::string` ? `"key"` is not a `std::string` in case that causes you confusion – 463035818_is_not_an_ai Nov 20 '19 at 12:46
  • 2
    Try: `mykey.c_str()` – John Nov 20 '19 at 12:46
  • @formerlyknownas_463035818 I guess that is confusing me then. What would it be in this case, an array of chars? – SomeDutchGuy Nov 20 '19 at 12:48
  • 1
    Does it work if you do this? `#define RAPIDJSON_HAS_STDSTRING 1`. See here for the [docs of the constructor](https://rapidjson.org/classrapidjson_1_1_generic_value.html#a852788b35257762cd8d6c4bf5c5d77bc) you're using. – acraig5075 Nov 20 '19 at 12:58
  • @RuudVerhoef string literal (not `std::string`, mind you, which is just library type from STL, i.e. it's not treated specially). – Dan M. Nov 20 '19 at 13:00
  • see here: https://stackoverflow.com/questions/15508148/what-is-the-type-of-a-string-literal-in-c (note that arrays decay to pointers when passed as parameter, hence you can pass a literal to a function expecting a `char*`) – 463035818_is_not_an_ai Nov 20 '19 at 13:04
  • @acraig5075 that solved it! I have no idea why as I thought I tried that already after having found that suggestion in an other topic I found using Google. – SomeDutchGuy Nov 20 '19 at 13:07

2 Answers2

4

You don't get support for std::string by default. rapidJSON requires you to specify you want std::string support.

#define RAPIDJSON_HAS_STDSTRING 1

Only then is this constructor you're using valid:

GenericValue (const std::basic_string< Ch > &s, Allocator &allocator)
acraig5075
  • 10,588
  • 3
  • 31
  • 50
2

JSON library you are using seems that doesn't work with string objects from standard library, but it works with const char*.

So you must convert string object to char* with the method c_str():

void setKeyValue(string myKey) {
        Value obj(kObjectType);
        Value key((char*)myKey.c_str());
        Value val(42);;
        obj.AddMember(key, val, d.GetAllocator());
    }
Jose Maria
  • 455
  • 2
  • 10
  • I just tried copying your answer however that might have solved the issue I had but still results in a red curly line underneath the same word saying: C++ no instance of constructor matches the argument list argument types are: (const char *). I am guessing I am still not passing in the correct type of data. – SomeDutchGuy Nov 20 '19 at 12:59
  • then you can force with a casting, I modify the solution – Jose Maria Nov 20 '19 at 13:14