本文介绍了C ++中的模板工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个传入类型的工厂,而不是对类型进行硬编码。但是,当我尝试将类型添加到类型.cpp文件中的工厂时,我将收到链接器错误。例如,他是我正在获得的链接器错误。

I am trying to make a factory that will have the type passed in, rather then having it hard coded for types. However, when I attempt to add the type to the factory inside of the types .cpp file, I will get a linker error. For example, he is a linker error I am currently getting.

1> RandomClass.obj:error LNK2019:未解析的外部符号public:short __thiscall TemplatedFactory :: AddType char*($ $ addType @ VRandomClass @@@ TemplatedFactory @@ QAEFPBD @ Z)在函数void _ cdecl中引用动态初始化程序'private:static short RandomClass :: ID''(void) (?? _E?ID @ RandomClass @@ 0FA @@ YAXXZ)

1>RandomClass.obj : error LNK2019: unresolved external symbol "public: short __thiscall TemplatedFactory::AddType(char const *)" (??$AddType@VRandomClass@@@TemplatedFactory@@QAEFPBD@Z) referenced in function "void _cdecl `dynamic initializer for 'private: static short RandomClass::ID''(void)" (??_E?ID@RandomClass@@0FA@@YAXXZ)

我试图让测试用例尽可能小,虽然它交叉五个文件,他们很小

I tried to make the test case as small as possible, though it does cross five files, they are very small

BaseClass.h:

BaseClass.h : http://codepad.org/MhZMw7t0

#pragma once
class BaseClass{ };

RandomClass.h:

RandomClass.h : http://codepad.org/xoObzP8G

#pragma once
#include "BaseClass.h"
class RandomClass : public BaseClass
{
private:
    static short ID;

public:
    RandomClass();
    virtual ~RandomClass();
};

TemplatedFactory.h:

TemplatedFactory.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);
};

RandomClass.cpp:

RandomClass.cpp : http://codepad.org/ALoF3Ysb

#include "RandomClass.h"
#include "TemplatedFactory.h"

short RandomClass::ID = TemplatedFactory::GetInstance().AddType<RandomClass>("RandomClass");

RandomClass::RandomClass() { }

RandomClass::~RandomClass() { }

TemplatedFactory.cpp:

TemplatedFactory.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;
}

如果我移动,我可以删除链接器错误

I can remove the the linker error if I move

short RandomClass::ID = TemplatedFactory::GetInstance().AddType<RandomClass>("RandomClass");

从RandomClass.cpp到TemplatedFactory.cpp,但是,我想在RandomClass中声明。 CPP。有没有人知道一种方法来解决这个问题,也可能是一个更好的设计(没有使用外部库)。

from RandomClass.cpp to TemplatedFactory.cpp, however, I would like to have the declaration in RandomClass.cpp. Does anyone know of a way to fix this or perhaps a better design (without the use of external libraries).

推荐答案

模板功能没有 export ,您的编译器可能不支持它们的定义和声明彼此分离。您需要将 TemplateFactory :: AddType 定义移动到标题。

Templated functions cannot have their definition and declaration separated from each other without export, which your compiler probably doesn't support. You need to move the TemplateFactory::AddType definition to the header.

这篇关于C ++中的模板工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:40