C++ - 将 rapidjson::Document 作为参数传递给函数
C++ - Passing rapidjson::Document as an argument to a function
我将指向 rapidjson::Document
的指针作为参数传递。
foo(rapidjson::Document* jsonDocument)
{
std::cout << jsonDocument["name"] << std::endl;
}
但是我无法jsonDocument["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);
我将指向 rapidjson::Document
的指针作为参数传递。
foo(rapidjson::Document* jsonDocument)
{
std::cout << jsonDocument["name"] << std::endl;
}
但是我无法jsonDocument["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);