为什么在复制rapidjson::Document时出现链接器错误而不是编译错误

Why linker error but not compile error when copying rapidjson::Document?

本文关键字:错误 编译 链接 复制 rapidjson Document 为什么      更新时间:2023-10-16

rapidjson::Document将结果复制到链接错误:

错误5错误LNK2019:未解析的外部符号"private:__thiscall rapidjson::GenericValue,class rapidjson::MemoryPoolAllocator>::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 rapidijson::MemoryPoolAllocator>const&)"中引用(??0$GenericDocument@U$UTF8@D@rapidjson@@V$MemoryPoolAllocator@VCrtAllocator@rapidjson@@@2@@rapidjson@@QAE@ABV01@@Z) C:\Layer.obj

我看到rapidjson::Documentrapidjson::GenericValue的子代它没有复制构造函数:

    //! Copy constructor is not permitted.
private:
    GenericValue(const GenericValue& rhs);

我想知道为什么没有编译器错误而是链接器错误?C++试图做什么?

我使用MVC 2013和rapidjson 0.11。这里还有类似的线程:

  1. LNK2019:";未解析的外部符号";使用rapidjson
  2. Rapidjson无法复制`Rapidjson::Document`

您已经部分回答了自己的问题:

    //! Copy constructor is not permitted.
private:
    GenericValue(const GenericValue& rhs);

所有类都有一个隐式复制构造函数:http://en.cppreference.com/w/cpp/language/copy_constructor#Implicitly-解密_副本_结构

此代码的作者试图通过在没有定义的情况下声明隐式复制构造函数来禁用它。通过声明,此代码可以进行编译。如果没有定义,它就无法链接,因此您可以看到自己的错误。

更具体地说,您看到的错误消息翻译如下:"GenericDocument类的隐式复制构造函数正在调用GenericValue类的显式复制构造函数。GenericValue中的复制构造函数是声明的,但没有定义。"您看到的文本以其自身的方式更具体,但显然更难阅读。

在您的代码中(可能是使用rapidjson的代码),存在对GenericDocument的复制构造函数的意外或故意调用,这给您带来了整个问题。在我的例子中,我将GenericDocument作为参数传递到函数中。如果你在做同样的事情,你应该通过引用传递文档,这样它就不会被复制。

该错误表示有一个函数已声明,但尚未实现。因此,您必须拥有somes.h,这些somes.h声明了一些函数,但在Rapidjson的任何地方都没有实现。