Reputation: 49
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!
Upvotes: 2
Views: 2048
Reputation: 1
To add to the above answer, Make sure the function is changed in your header file or your function declaration above main too! I made the mistake of not changing my header file and spent 2 hours figuring out :( change function declaration like below!
void add_rules_to_tester(rapidjson::Document& document) //add &!!
Upvotes: 0
Reputation: 917
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);
Upvotes: 3