为什么复制 rapidjson::Document 时链接器错误而不是编译错误?
Why linker error but not compile error when copying rapidjson::Document?
rapidjson::Document
复制结果到link错误:
Error 5 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) C:\Layer.obj
我看到 rapidjson::Document
是 rapidjson::GenericValue
的 child
没有复制构造函数:
//! Copy constructor is not permitted.
private:
GenericValue(const GenericValue& rhs);
我想知道为什么没有编译错误而是link错误? C++ 试图做什么?
我使用 MVC 2013,并且 rapidjson 0.11。这里也有类似的话题:
- LNK2019: "Unresolved external symbol" with rapidjson
- Rapidjson cannot copy `rapidjson::Document`
该错误表示声明了一个函数但未实现。
所以你必须有一些 .h 声明了一些函数,但没有在你拥有的 Rapidjson 中的任何地方实现。
您已经部分回答了自己的问题:
//! Copy constructor is not permitted.
private:
GenericValue(const GenericValue& rhs);
所有 类 都有一个隐式复制构造函数:
http://en.cppreference.com/w/cpp/language/copy_constructor#Implicitly-declared_copy_constructor
此代码的作者试图通过在没有定义的情况下声明它来禁用隐式复制构造函数。通过声明,此代码可以编译。没有定义,它就不能 link,因此您会看到错误。
更具体地说,您看到的错误消息翻译如下:"The implicit copy-constructor of the GenericDocument class is calling the implicit copy-constructor of the GenericValue class. The copy-constructor in the GenericValue class is declared but not defined."您看到的文本以其自己的方式更具体,但显然更难阅读。
在您的代码中(可能是使用 rapidjson 的代码),存在对 GenericDocument 的复制构造函数的意外或故意调用,这导致了整个问题。在我的例子中,我将 GenericDocument 作为参数传递给函数。如果您正在做同样的事情,您应该通过引用传递文档,这样它就不会被复制。
rapidjson::Document
复制结果到link错误:
Error 5 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) C:\Layer.obj
我看到 rapidjson::Document
是 rapidjson::GenericValue
的 child
没有复制构造函数:
//! Copy constructor is not permitted.
private:
GenericValue(const GenericValue& rhs);
我想知道为什么没有编译错误而是link错误? C++ 试图做什么?
我使用 MVC 2013,并且 rapidjson 0.11。这里也有类似的话题:
- LNK2019: "Unresolved external symbol" with rapidjson
- Rapidjson cannot copy `rapidjson::Document`
该错误表示声明了一个函数但未实现。 所以你必须有一些 .h 声明了一些函数,但没有在你拥有的 Rapidjson 中的任何地方实现。
您已经部分回答了自己的问题:
//! Copy constructor is not permitted.
private:
GenericValue(const GenericValue& rhs);
所有 类 都有一个隐式复制构造函数: http://en.cppreference.com/w/cpp/language/copy_constructor#Implicitly-declared_copy_constructor
此代码的作者试图通过在没有定义的情况下声明它来禁用隐式复制构造函数。通过声明,此代码可以编译。没有定义,它就不能 link,因此您会看到错误。
更具体地说,您看到的错误消息翻译如下:"The implicit copy-constructor of the GenericDocument class is calling the implicit copy-constructor of the GenericValue class. The copy-constructor in the GenericValue class is declared but not defined."您看到的文本以其自己的方式更具体,但显然更难阅读。
在您的代码中(可能是使用 rapidjson 的代码),存在对 GenericDocument 的复制构造函数的意外或故意调用,这导致了整个问题。在我的例子中,我将 GenericDocument 作为参数传递给函数。如果您正在做同样的事情,您应该通过引用传递文档,这样它就不会被复制。