简体   繁体   中英

Template class- unresolved externals c++ SFML

I have a template class ResourceHolder which is supposed to carry "Resources". The function's declarations are in ResourceHolder.inl file. I have followed the book "SFML Game Development Book" and as I can see all that they have done in their ResourceHolder class is almost the same as mine. Here are links to their ResourceHolder.h: https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/07_Gameplay/Include/Book/ResourceHolder.hpp

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

But I get unresolved externals whenever I try to create an instance of that class.

Here's my 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;
 }

And also I have ResourceIdentifiers.h file where I keep some typedefs...:

#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

I use enums for Identifier and sf::Texture or sf::Font for Resource.

You havent implemented the constructor nor the destructor. Therefor the linker tells you that he is lacking the definition of "external symbols" (ctor and dtor in this case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM