我将指针传递rapidjson::Document
为参数。
foo(rapidjson::Document* jsonDocument)
{
std::cout << jsonDocument["name"] << std::endl;
}
但是我无法jsonDocument["name"]
访问name属性。
尝试不使用指针会导致错误:
error: 'rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>::GenericDocument(const rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; StackAllocator = rapidjson::CrtAllocator]' is private
GenericDocument(const GenericDocument&);
有人能帮我吗?
使用引用或值作为参数。使用[]
带有指针会尝试使用您的文档,如果它是文件的数组。引用或值将调用期望的运算符。
// a const reference
foo(const rapidjson::Document& jsonDocument) {
std::cout << jsonDocument["name"] << std::endl;
}
// a copy (or move)
foo(rapidjson::Document jsonDocument) {
std::cout << jsonDocument["name"] << std::endl;
}
我建议您使用引用,因为您的函数不需要消耗文档中的任何资源,而只需观察并打印一个值。
该函数的调用将如下所示:
rapidjson::Document doc = /* ... */;
foo(doc);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句