msvc++链接器错误,我不太理解

MSVC++ Linker error which I do not quite understand

本文关键字:链接 错误 msvc++      更新时间:2023-10-16

正确的,我得到奇怪的链接错误,我从来没有见过,我不能真正破译。

RelationOwnerRelationUser的代码可以在这里找到。注意:我已经将所有函数体移到了源文件中,而不是头文件中。命名保持不变。

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

第一个是关于构造函数的,第二个是关于析构函数的。我明白了。

我还实现了我自己的UserOwner版本,如下所示:

// 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之后。我把实现移回头文件,它工作了。

我想重复的问题是正确的。