我有一个模板类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用于资源。

最佳答案

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

07-24 14:40