添加字符串问题
正常传入字符串编译通过:
rapidjson::Value temp(rapidjson::kObjectType);
temp.AddMember("name", "xiaofang", allocator);
但传入字符变量编译错误:
std::string name = "xiaofang";
rapidjson::Value temp(rapidjson::kObjectType);
temp.AddMember("name", name.c_str(), allocator);
编译通过的方法一:
rapidjson::Value temp(rapidjson::kObjectType);
std::string name = "xiaofang";
rapidjson::Value val;
temp.AddMember("name", val.SetString(name.c_str(),allocator), allocator);
编译通过的方法二:
rapidjson::Value temp(rapidjson::kObjectType);
std::string name = "xiaofang";
temp.AddMember("name", rapidjson::StringRef(name.c_str(), name.size()), allocator);
使用手册:对于字符指针,RapidJSON需要作一个标记,代表它不复制也是安全的。可以使用StringRef函数。
虽然编译通过了,但有没有问题有待考察,暂时先用着。