I'm following a book about SFML game development, but I'm stuck on the second chapter, because I can't compile the code I just wrote.
It's almost word-by-word copy from the book (except from member variable name and exception text). I have experience with C++ and templates, but I have never seen this error before and I've been staring at this for few hours now and I don't see anything wrong with this code.
Here is my *.h file:
#pragma once
#include <map>
#include <memory>
#include <string>
#include <stdexcept>
#include <cassert>
#include "enumFile.h"
template <typename Resource, typename Identifier>
class ResourceHolder
{
public:
ResourceHolder(void);
~ResourceHolder(void);
void load(Identifier id, const std::string & filename);
template <typename Parameter>
void load(Identifier id, const std::string & filename,
const Parameter& secondParam);
Resource & get(Identifier id);
const Resource & get(Identifier id) const;
private:
std::map<Identifier, std::unique_ptr<Resource>> resourceMap;
};
#include "ResourceHolder.inl"
and here is my *.inl file:
template <typename Resource, typename Identifier>
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& filename)
{
// Create and load resource
std::unique_ptr<Resource> resource(new Resource());
if (!resource->loadFromFile(filename))
throw std::runtime_error("Failed to load resource: " + filename);
// If loading successful, insert resource to map
auto inserted = resourceMap.insert(std::make_pair(id, std::move(resource)));
assert(inserted.second);
}
template <typename Resource, typename Identifier>
template <typename Parameter>
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& filename,
const Parameter& secondParam)
{
// Create and load resource
std::unique_ptr<Resource> resource(new Resource());
if (!resource->loadFromFile(filename, secondParam))
throw std::runtime_error("Failed to load resource: " + filename);
// If loading successful, insert resource to map
auto inserted = resourceMap.insert(std::make_pair(id, std::move(resource)));
assert(inserted.second);
}
template <typename Resource, typename Identifier>
Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
auto found = resourceMap.find(id);
assert(found != resourceMap.end());
return *found->second;
}
template <typename Resource, typename Identifier>
const Resource& ResourceHolder<Resource, Identifier>::get(Identifier id) const
{
auto found = resourceMap.find(id);
assert(found != resourceMap.end());
return *found->second;
}
Sorry for a lot of code, but I'm desperate here. I'm getting unusual errors, all in the *.inl file, instead of writing them, I took a screenshot:
Any idea how to fix this?
EDIT: a word on how the class is used.
I have and enum inside a texture namespace (that is what "enumFile.h" is)
namespace Textures
{
enum ID {Landscape, Airplane, Missile};
}
When I want to use the class, I use it as follows:
ResourceHolder<sf::Texture, Textures::ID> textureHolder;
textureHolder.load(Textures::Airplane, "path/to/texture.png");