c++ rapidjson return value
c++ rapidjson return value
我在项目中使用rapidjson。我有一个解析json并返回部分json的方法
static rapidjson::Document getStructureInfo(std::string structureType)
{
rapidjson::Document d = getStructuresInfo();
rapidjson::Document out;
out.CopyFrom(d[structureType.c_str()], d.GetAllocator());
std::string title1 = out["title"].GetString();
return out;
}
然后,我用这个部分从中得到一个值。
rapidjson::Document info = StructureManager::getStructureInfo(type);
title2=info["title"].GetString();
问题是标题1被成功读取,但标题2在文档中的以下行面临访问违规问题。h:
bool IsString() const { return (flags_ & kStringFlag) != 0; }
我想知道退回部分文件的正确方式是什么。(我不想使用指针)。
感谢
Value
的(const)引用即可。
static rapidjson::Value& getStructureInfo(std::string structureType)
{
return d[structureType.c_str()];
}
rapidjson::Value& info = StructureManager::getStructureInfo(type);
title2=info["title"].GetString();
顺便说一句,原始代码中的问题是由于d.GetAllocator()
属于局部变量d
,当局部变量被破坏时,分配将变得无效。下面的解决方案应该可以修复它,但我推荐上面的解决方案,它使用引用来防止复制。
out.CopyFrom(d[structureType.c_str()], out.GetAllocator());
相关文章:
- 方法错误"not all control paths return a value"和方法不返回值
- 如何解决类成员函数中的"return a value"错误?
- QThread start() return value
- 警告:"Not all control paths return a value" c++
- What value should `std::stringstream::fail()` return after r
- Return value of std::hash ofr (x86/x64)
- 在这个程序中"getavg must return value"意味着什么?
- OpenCV HOGDescriptor return value
- 虚假"<function> is expected to return a value"警告?
- Opencv cv::waitKey() return value
- 我返回一个值,但编译器告诉我"function must return a value"
- const object&return value from a method, best practice
- 循环函数给出"return’ with a value, in function returning void"误差
- Return-value C++
- 计算距离:方法"must return a value" ?
- c++ rapidjson return value
- C++错误"not all control paths return a value"
- 当您使用 std::move 返回时,"return-by-rvalue-ref" 和 "return-by-value" 之间的区别?
- CAsyncSocket receive() return value
- boost program_options add_options return value