【问题标题】:LNK2019: "Unresolved external symbol" with rapidjsonLNK2019:带有 rapidjson 的“未解析的外部符号”
【发布时间】:2014-05-06 03:00:24
【问题描述】:

我有一个 Visual C++ 项目,我在其中添加了 rapidjson 库,该库经过测试可以正常工作。但是当我在嵌套类中添加rapidjson::Document 类型时,当我尝试编译时会抛出LNK2019 错误。该项目是用于创建 DLL 的动态库。

这是我的 ma​​in.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);
};

这是错误:

错误 LNK1120:1 个未解决的外部问题

错误 LNK2019:无法解析的外部符号“私有:__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) 在函数“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)

当我在代码中注释用 HERE IS THE PROBLEM 注释的行时,错误消失。如您所见,在coreBD 类中使用test 变量不会导致错误。嵌套类中仅存在rapidjson::Document 类型的变量会导致显示错误;用不用都无所谓。

可能是什么问题?


编辑

收集到的新信息。

当我在父类中使用嵌套类时会出现问题,但仅限于方法的return。换句话说:我可以使用rapidjson::Document 类型作为成员变量创建所有内容,我可以在coreBD 类中创建一个jsonObj 类型的方法,我可以在该方法中实例化jsonObj但我不能如果类jsonObj 声明了rapidjson::Document 成员变量,则返回jsonObj 类型的值。

例如这个新创建的方法:

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
}

编辑

新问题继续解决这个问题:Perform a copy of Document object of rapidjson

【问题讨论】:

  • 添加 rapidjason 的库文件作为对项目属性的引用。
  • @Trinity rapidjson 只是标题,它们包含在我的项目中,#includes,正如您所读到的,该库在我项目的所有其他部分都正常工作
  • 这是一个单独的解决方案吗?这意味着您的应用程序的其他工作部分被放置在您项目的不同解决方案中。
  • @XAMlMAX 不。一种独特的解决方案。如果这很重要,我也有一个 C++/CLI 包装器。该项目是一个动态库。
  • 您是否在项目中的其他任何地方使用此rapidjson::Document?我知道您说它在解决方案中的其他任何地方都可以使用,但 rapidjson::Document 是否也可以在其他任何地方使用?

标签: c++ visual-c++ visual-studio-2012 rapidjson


【解决方案1】:

jsonObj 没有复制构造函数,也不能有任何复制构造函数,因为在 rapidjson 中禁用了 Document 的复制构造函数。尝试保持指向文档的指针,如下所示:

class jsonObj {
    string jsonStr;
    string message;
    Document* doc; //HERE IS THE PROBLEM
    bool validMsg;
}

或将文件(jsonObj)从外部传递到:

jsonObj query(string sSQL);

例如:

query(string sSQL, jsonObj & out_obj)

【讨论】:

    【解决方案2】:

    查看错误,返回jsonObj 的函数似乎正在执行某种复制或移动构造,作为返回值的一部分,而底层类可能通过使这些构造函数成为私有成员而不允许这样做。

    有些类的设计要求禁止复制或赋值,以防止内存泄漏,或者因为对象是单例类型对象并且只允许对象的一个​​版本。

    看看这个documentation on rapidjson,在移动语义部分中有一个可能是相关的注释。看起来他们正在阻止复制以提高性能。

    【讨论】:

    • 看起来文档中与 Document 对象相关的部分丢失了。
    • @SysDragon,我认为你是对的,文档似乎不完整。然而,从顶部向下大约 2/3 处有一个题为“移动语义”的部分,讨论了分配、复制和移动。一般来说,文档似乎很简陋。 document.h 包含文件中有一条注释,不允许复制构造函数,请参见第 54 行 code.google.com/p/rapidjson/source/browse/trunk/include/…
    • 这就是我现在遇到的问题。我无法定义复制构造函数,因为“无法访问私有成员”。我正在尝试使用Document.Accept() 方法,它似乎在执行复制:stackoverflow.com/a/19604013/1967056
    • 我认为I'm going to create a new question 因为这与原来的问题已经发生了很大的不同,而且增长得太快了。谢谢你的帮助。查看我的编辑。
    猜你喜欢
    • 2014-11-07
    • 2013-06-01
    • 1970-01-01
    • 2022-01-06
    • 2014-09-19
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多