声明模板类后的C ++链接器错误

时间:2013-07-30 18:12:52

标签: c++ resources game-engine sfml

我正在为SFML对象制作某种形式的资源管理器,并防止重复代码我认为我会聪明并使用模板。但这并不顺利。

我创建了一个名为ResourceManager的基类,它是一个模板类。它包含一个字符串映射作为键和唯一指针,以键入T作为值。该类有一个虚拟析构函数和一个虚拟加载方法,用于加载不同的资源,它有两个用于获取或擦除映射值的方法geterase

该课程如下:

#include <string>
#include <map>
#include <memory>

template<typename T>
class ResourceManager
{
protected:
    std::string m_path;
    std::map< std::string , std::unique_ptr< T > >  resourceMap;
    virtual bool load(std::string &name) = 0;
public:
    ResourceManager(const std::string &path);
    virtual ~ResourceManager();
    T& get(const std::string &name);
    void erase(const std::string &name);
};

template<typename T>
ResourceManager<T>::ResourceManager(const std::string &path ) : m_path(path)
{}


template<typename T>
T& ResourceManager<T>::get(const std::string &name )
{
    if(resourceMap.empty() || resourceMap.find(name) == resourceMap.end())
    {
        if(!load(name))
        {
            std::cout << "Couldn't load resource " << name << std::endl;
            std::exit(EXIT_FAILURE);
        }
    }
    return *resourceMap.at(name);
}


template<typename T>
void ResourceManager<T>::erase(const std::string &name )
{
    resourceMap.erase(name);
}

然后有单独的SFML资源管理器继承ResourceManager类并实现load方法。以下是其中一个示例:

#include <string>
#include <iostream>

#include <SFML\Graphics\Font.hpp>

#include "ResourceManager.h"

class FontManager : public ResourceManager<sf::Font>
{
private:
    bool load(std::string &name);
public:
    FontManager(const std::string path);
    ~FontManager();
};

FontManager::FontManager(const std::string path ) : ResourceManager(path)
{}

FontManager::~FontManager()
{}

bool FontManager::load( std::string &name )
{
    resourceMap.insert(std::make_pair(name, std::unique_ptr<sf::Font>(new sf::Font)));
    return resourceMap.at(name)->loadFromFile(m_path + "\\" + name);
}

我认为这应该可行,但在构建时我会收到以下错误

Error   1   error LNK2019: unresolved external symbol "public: __thiscall ResourceManager<class sf::Font>::ResourceManager<class sf::Font>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$ResourceManager@VFont@sf@@@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall FontManager::FontManager(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0FontManager@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) FontManager.obj

Error   2   error LNK2019: unresolved external symbol "public: virtual __thiscall ResourceManager<class sf::Font>::~ResourceManager<class sf::Font>(void)" (??1?$ResourceManager@VFont@sf@@@@UAE@XZ) referenced in function "public: virtual __thiscall FontManager::~FontManager(void)" (??1FontManager@@UAE@XZ)   FontManager.obj 

Error   3   error LNK2019: unresolved external symbol "public: class sf::Font & __thiscall ResourceManager<class sf::Font>::get(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?get@?$ResourceManager@VFont@sf@@@@QAEAAVFont@sf@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: virtual void __thiscall GameStateIngame::draw(void)" (?draw@GameStateIngame@@UAEXXZ)   GameStateIngame.obj 

Error   4   error LNK2001: unresolved external symbol "public: class sf::Font & __thiscall ResourceManager<class sf::Font>::get(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?get@?$ResourceManager@VFont@sf@@@@QAEAAVFont@sf@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)    GameStateIntro.obj

Error   5   error LNK2001: unresolved external symbol "public: class sf::Font & __thiscall ResourceManager<class sf::Font>::get(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?get@?$ResourceManager@VFont@sf@@@@QAEAAVFont@sf@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)    GameStateMenu.obj

解释一些可能导致混淆的事情。管理器在名称空间内被声明为全局变量,位于Globals.h / Globals.cpp中,它们在这里初始化为:

//Globals.h
namespace resource
{
    extern FontManager fontManager;
}

//Globals.cpp
namespace resource
{
    FontManager fontManager("res\\");
}

在GameState对象内部调用这样的方法:

using namespace resource;
sf::Text text("Ingame State", fontManager.get("LeagueGothic-Regular.otf"), 70U);

这些错误不是由于我的SFML库链接错误,因为没有我的资源管理类SFML组件按预期工作。我的资源管理类有些问题,我无法弄清楚是什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

学习如何从编译器和链接器读取错误消息非常重要。在这种情况下,链接器抱怨:

  

错误2错误LNK2019:未解析外部符号“public:virtual __thiscall ResourceManager :: ~ResourceManager(void)”(?? 1?$ ResourceManager @ VFont @sf @@@@ UAE @ XZ)在函数“public:virtual __thiscall FontManager :: ~FontManager(void)”(?? 1FontManager @@ UAE @ XZ)中引用FontManager.obj

析构函数的定义在哪里?

使用您发布的代码解释其他错误有点难度。是否在声明模板的标题中定义了get成员函数?