问题
I have a Visual C++ Project in which I added the rapidjson library, which is tested to be working properly. But when I add a rapidjson::Document
type to the nested class is throwing a LNK2019
error when I try to compile. The project is a dynamic library to create a DLL.
This are the definitions in my main.h:
class coreBD {
string conn;
string proxy;
int type;
Document test;
enum dataBases {
Sqlite,
SqlServer,
None
};
string queryBD(string sSQL);
string queryHTTP(string sSQL);
string httpRequest(string url, string proxy);
static string getNow(string format);
static string urlEncode(string url);
static bool startsWith(string source, string with);
public:
enum access {
dbConn,
HTTPProtocol
};
//Nested class
class jsonObj {
string jsonStr;
string message;
Document doc; //HERE IS THE PROBLEM
bool validMsg;
public:
enum response {
FullResponse,
SQLResponse
};
jsonObj(string json);
string getJsonStr(response rType);
string getErrorMsg();
bool isValidResponse();
};
coreBD(string connStr, access connType);
jsonObj query(string sSQL);
void setProxy(string proxy);
};
This is the error:
error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol "private: __thiscall rapidjson::GenericValue,class rapidjson::MemoryPoolAllocator >::GenericValue,class rapidjson::MemoryPoolAllocator >(class rapidjson::GenericValue,class rapidjson::MemoryPoolAllocator > const &)" (??0?$GenericValue@U?$UTF8@D@rapidjson@@V?$MemoryPoolAllocator@VCrtAllocator@rapidjson@@@2@@rapidjson@@AAE@ABV01@@Z) referenced in function "public: __thiscall rapidjson::GenericDocument,class rapidjson::MemoryPoolAllocator >::GenericDocument,class rapidjson::MemoryPoolAllocator >(class rapidjson::GenericDocument,class rapidjson::MemoryPoolAllocator > const &)" (??0?$GenericDocument@U?$UTF8@D@rapidjson@@V?$MemoryPoolAllocator@VCrtAllocator@rapidjson@@@2@@rapidjson@@QAE@ABV01@@Z)
The error disappears when I comment the line commented with HERE IS THE PROBLEM in the code. As you can see, the use of the test
variable in the coreBD
class causes no error. The mere existence of the variable of type rapidjson::Document
in the nested class causes de error to show; it doesn't matter if I use it or not.
What could be the problem?
EDIT:
New information gathered.
The problem appears when I use the nested class inside the parent one, but only in the return
of a method. In other words: I can create everything with rapidjson::Document
type as a member variable, I can create a method in coreBD
class with type jsonObj
, I can instantiate jsonObj
inside that methods, but I cannot return a value of type jsonObj
if the class jsonObj
has a rapidjson::Document
member variable declared.
For example this new created method:
jsonObj coreBD::testOBJ()
{
string json = "{error:null, message:None, errorMessage:MoreNone}";
jsonObj b(json);
return b; //It fails here if I return a nested class with a rapidjson::Document in it. Returning NULL works
}
EDIT:
New question continuing solving this: Perform a copy of Document object of rapidjson
回答1:
Looking at the error it appears that the function returning the jsonObj
is doing some kind of a copy or move construction as a part of returning the value and the underlying classes do not allow this probably by making those constructors private members.
There are classes whose design requires that a copy or assignment is prohibited in order to prevent memory leaks or because the objects are singleton type objects and only one version of the object is allowed.
Looking at this documentation on rapidjson there is a note in the section on Move semantics that may be pertinent. It looks like they are preventing a Copy in order to improve performance.
回答2:
jsonObj
doesn't have copy constructor and it can't have any copy constructor since Document's copy constructor is disabled in rapidjson. Try to hold pointer to document instead, something like this :
class jsonObj {
string jsonStr;
string message;
Document* doc; //HERE IS THE PROBLEM
bool validMsg;
}
Or pass document(jsonObj
) from outside to:
jsonObj query(string sSQL);
For example:
query(string sSQL, jsonObj & out_obj)
来源:https://stackoverflow.com/questions/22682513/lnk2019-unresolved-external-symbol-with-rapidjson