Reputation: 107
I'm passing pointer to rapidjson::Document
as an argument.
foo(rapidjson::Document* jsonDocument)
{
std::cout << jsonDocument["name"] << std::endl;
}
But I cannot do jsonDocument["name"]
to access the name attribute.
Attempting to not use pointers leads to an error:
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&);
Can someone help me?
Upvotes: 4
Views: 4557
Reputation: 41780
Use a reference or a value as argument. Using the []
with a pointer will try to use your document as if it was an array of document. A reference or a value will call the expected operator.
// 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;
}
I'd recommend you to use the reference, as your function don't need to consume any resources in the document, but only observe and print a value.
The call of this function will look like this:
rapidjson::Document doc = /* ... */;
foo(doc);
Upvotes: 5