我正在尝试创建一个将传入类型的工厂,而不是针对类型进行硬编码。但是,当我尝试将类型添加到类型 .cpp 文件中的工厂时,我将收到链接器错误。比如他是我目前得到的一个linker error。
1>RandomClass.obj:错误 LNK2019:未解析的外部符号“public: short __thiscall TemplatedFactory::AddType(char const *)” (??$AddType@VRandomClass@@@TemplatedFactory@@QAEFPBD@Z) 在函数中引用"void _cdecl `private: static short RandomClass::ID''(void) 的动态初始值设定项"(??_E?ID@RandomClass@@0FA@@YAXXZ)
我试图让测试用例尽可能小,虽然它确实跨越五个文件,但它们非常小
基类.h:http://codepad.org/MhZMw7t0
#pragma once
class BaseClass{ };
随机类.h:http://codepad.org/xoObzP8G
#pragma once
#include "BaseClass.h"
class RandomClass : public BaseClass
{
private:
static short ID;
public:
RandomClass();
virtual ~RandomClass();
};
模板化工厂.h:http://codepad.org/qkcTBw24
#pragma once
#include <map>
using std::map;
#include "BaseClass.h"
template<typename Type> BaseClass* createType() { return new Type; }
class TemplatedFactory
{
private:
typedef BaseClass* (*ComponentFactoryFuncPtr)();
typedef map<const char*, ComponentFactoryFuncPtr> map_type;
map_type m_Map;
public:
static TemplatedFactory &GetInstance();
template<typename Type>
short AddType(const char* componentName);
};
随机类.cpp:http://codepad.org/ALoF3Ysb
#include "RandomClass.h"
#include "TemplatedFactory.h"
short RandomClass::ID = TemplatedFactory::GetInstance().AddType<RandomClass>("RandomClass");
RandomClass::RandomClass() { }
RandomClass::~RandomClass() { }
模板化工厂.cpp:http://codepad.org/iqgNqa6H
#include "TemplatedFactory.h"
TemplatedFactory &TemplatedFactory::GetInstance()
{
static TemplatedFactory instance;
return instance;
}
template<typename Type>
short TemplatedFactory::AddType(const char* componentName)
{
ComponentFactoryFuncPtr function = &createType<Type>;
m_Map.insert(std::make_pair(componentName, function));
return 0;
}
如果我移动,我可以删除链接器错误
short RandomClass::ID = TemplatedFactory::GetInstance().AddType<RandomClass>("RandomClass");
从 RandomClass.cpp 到 TemplatedFactory.cpp,但是,我希望在 RandomClass.cpp 中有声明。有谁知道解决这个问题的方法或者更好的设计(不使用外部库)。
最佳答案
如果没有 export
,模板函数不能将它们的定义和声明彼此分开,您的编译器可能不支持。您需要将 TemplateFactory::AddType
定义移动到 header 。
关于c++ - C++中的模板工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5185502/