msvc++链接器错误,我不太理解
MSVC++ Linker error which I do not quite understand
正确的,我得到奇怪的链接错误,我从来没有见过,我不能真正破译。
RelationOwner
和RelationUser
的代码可以在这里找到。注意:我已经将所有函数体移到了源文件中,而不是头文件中。命名保持不变。
Error 1 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::RelationUser<class Family,class Citizen>(class Family *)" (??0?$RelationUser@VFamily@@VCitizen@@@@QAE@PAVFamily@@@Z) referenced in function "public: __thiscall Citizen::Citizen(class Family &,class Name)" (??0Citizen@@QAE@AAVFamily@@VName@@@Z) *path*Citizen.obj CodeAITesting
Error 2 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::~RelationUser<class Family,class Citizen>(void)" (??1?$RelationUser@VFamily@@VCitizen@@@@QAE@XZ) referenced in function __unwindfunclet$??0Citizen@@QAE@AAVFamily@@VName@@@Z$1 *path*Citizen.obj CodeAITesting
Error 3 error LNK2019: unresolved external symbol "public: __thiscall RelationOwner<class Family,class Citizen>::~RelationOwner<class Family,class Citizen>(void)" (??1?$RelationOwner@VFamily@@VCitizen@@@@QAE@XZ) referenced in function __unwindfunclet$??0Family@@QAE@VName@@@Z$1 *path*Family.obj CodeAITesting
第一个是关于构造函数的,第二个是关于析构函数的。我明白了。
我还实现了我自己的User
和Owner
版本,如下所示:
// header of Family.h (Owner in the linked PDF file)
#include "Name.h"
#include "RelationOwner.h"
class Citizen;
class Family; // I didn't really know if this one was necessary.
class Family
: public RelationOwner<Family, Citizen>
{
public:
Family(Name name);
private:
Name name;
};
// Source of Family.cpp
#include "Name.h"
Family::Family(Name name)
: name(name)
{
}
//Source of Citizen.h (User in the linked PDF)
#include "Name.h"
#include "RelationUser.h"
class Citizen;
class Family;
class Citizen
: public RelationUser<Family, Citizen>
{
public:
Citizen(Family &family, Name name);
private:
Name name;
};
// Source of Citizen.cpp
#include "Family.h"
#include "Name.h"
#include "RelationUser.h"
Citizen::Citizen(Family &family, Name name)
: RelationUser<Family, Citizen>(&family),
name(name)
{
}
据我所知,我没有做任何花哨的事情,但它一直在抱怨,我不知道为什么。
正如Brian Beuning告诉我的"通常模板的代码都在头文件中"。读过n.m之后。我把实现移回头文件,它工作了。
我想重复的问题是正确的。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误