模板类 - 未解析的外部c ++ SFML

时间:2014-03-02 13:56:01

标签: c++ templates sfml

我有一个模板类ResourceHolder,它应该带有“Resources”。函数的声明在ResourceHolder.inl文件中。我已经阅读了“SFML游戏开发手册”这本书,我可以看到他们在ResourceHolder课上所做的一切与我的几乎相同。以下是他们的ResourceHolder.h的链接:https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/07_Gameplay/Include/Book/ResourceHolder.hpp

和ResourceHolder.inl:https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/07_Gameplay/Include/Book/ResourceHolder.inl

但是每当我尝试创建该类的实例时,我都会得到未解析的外部因素。

这是我的ResourceHolder.h:

#pragma once
#include <map>
#include <memory>
#include <string>

template<typename Identifier, typename Resource>
class ResourceHolder
{
  public:

     ResourceHolder();

    ~ResourceHolder();

     void load(Identifier, std::string);

     Resource& get(Identifier);

  private:

     std::map<Identifier, std::unique_ptr<Resource>> mResources;

};


  #include "ResourceHolder.inl"

ResourceHolder.inl:

template<typename Identifier, typename Resource>
void ResourceHolder<Identifier, Resource>::load(Identifier id, std::string pathToFile)
{
std::unique_ptr<Resource> resource(new Resource());

if (!resource->loadFromFile(pathToFile))
{
    //Didn't find any "Resource" at that location!

}

mResources.insert(std::pair<Identifier, Resource>(id, std::move(resource)));
}


 template<typename Identifier, typename Resource>
 Resource& ResourceHolder<Identifier, Resource>::get(Identifier id)
 {
   auto found = mResources.find(id);

   return *found->second;
 }

我还有ResourceIdentifiers.h文件,我保留了一些typedef ...:

#ifndef RESOURCEIDENTIFIERS_H
#define RESOURCEIDENTIFIERS_H

namespace sf
{
   class Texture;
   class Font;
}

namespace Textures
{
    enum ID
    {
        Eagle,
        Raptor,
        Desert,
        TextureCount
    };
}

 namespace Fonts
 {
    enum ID
    {
        Arial
    }; 
 }

 template<typename Identifier, typename Resource>
 class ResourceHolder;

 typedef ResourceHolder<Textures::ID, sf::Texture> TextureHolder;
 typedef ResourceHolder<Fonts::ID, sf::Font> FontHolder;

 #endif

我使用标识符和sf :: Texture的枚举或资源的sf :: Font。

1 个答案:

答案 0 :(得分:0)

您尚未实现构造函数或析构函数。因此链接器告诉你他缺乏“外部符号”的定义(在这种情况下是ctor和dtor)。