我正在为 SFML 对象制作某种形式的资源管理器,并防止重复代码,我认为我会很聪明并使用模板。然而,这并不顺利。
我创建了一个名为ResourceManager
模板类的基类。它包含作为键的字符串映射和作为值的类型 T 的唯一指针。该类有一个虚拟析构函数和一个虚拟加载方法,用于加载不同的资源,它有两个内部方法get
,erase
用于从映射中获取或擦除值。
该类如下所示:
#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
类并实现加载方法。这是其中之一的示例:
#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 组件按预期工作。我的资源管理课程出了点问题,我不知道是什么问题。任何人都可以帮忙吗?