I have a C++ class Tester.cpp that uses the rapidjson JSON parser.
Here is an abbreviated version of the code:
using namespace std;
using namespace rapidjson;
int main(int argc, char** argv)
{
...
//Parse the JSON
rapidjson::Document document;
document.Parse(buffer);
add_rules_to_tester(document);
...
}
void add_rules_to_tester(rapidjson::Document document)
{...}
My header file Tester.h looks like the below (again abbreviated):
using namespace std;
using namespace rapidjson;
void add_rules_to_tester(rapidjson::Document document);
When I comment out the line add_rules_to_tester in the main method, I do not get any errors. When I uncomment that line, I get the below compile-time errors.
In file included from Tester.h:38:0,
from Tester.cpp:28:
rapidjson/document.h: In function ‘int main(int, char**)’:
rapidjson/document.h:2076:5: 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&);
^
Tester.cpp:163:34: error: within this context
add_rules_to_tester(document);
^
In file included from Tester.cpp:28:0:
Tester.h:76:6: error: initializing argument 1 of ‘void add_rules_to_tester(rapidjson::Document)’
void add_rules_to_tester(rapidjson::Document document);
Any suggestions on what the problem could be? It seems to me that I have misunderstood the use of namespaces somehow, but let me know if there's any other information I can provide. Thanks!
rapidjson/document.h: In function ‘int main(int, char**)’:
rapidjson/document.h:2076:5: 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&);
The compiler tells you that copy constructor for GenericDocument is declared as private, so you can not use it outside of GenericDocument class.
You are calling copy constructor at this statement while passing it as argument by creating copy :
add_rules_to_tester(document);
Solution:
Pass document
object by reference.
void add_rules_to_tester(rapidjson::Document& document) //Note & here
{...}
And call it as add_rules_to_tester(document);