I've been reading the book SFML Game Development, and I wanted to create a template class. I've separated it in 2 files: a .h file and a .inl file like so:
// ResourceHolder.h
#ifndef _RES_HOLDER
#define _RES_HOLDER
#include <map>
#include <memory>
#include <SFML/Graphics.hpp>
namespace Resources
{
enum ID {LandscapeTexture, AirplaneTexture, MissileTexture};
template<typename Resource>
class ResourceHolder
{
public:
void load(Resources::ID id, const std::string& file);
template<typename Parameter>
void load(Resources::ID id, const std::string&file,
const Parameter& param);
Resource& get(Resources::ID id);
const Resource& get(Resources::ID id) const;
private:
std::map<Resources::ID, std::unique_ptr<Resource>> mResourceMap;
};
#include "ResourceHolder.inl"
};
#endif
.
//ResourceHolder.inl
template<typename Resource>
void Resources::ResourceHolder<Resource>::load(Resources::ID id,
const std::string& file)
{
std::unique_ptr<Resource> resource(new Resource());
if(!resource->loadFromFile(file))
throw std::runtime_error("ResourceHolder::load failed to load : "+file);
mResourceMap.insert(std::make_pair(id, std::move(resource)));
}
template<typename Resource>
template<typename Parameter>
void Resources::ResourceHolder<Resource>::load(Resources::ID id,
const std::string& file,
const Parameter& param)
{
std::unique_ptr<Resource> resource(new Resource());
if(!resource->loadFromFile(file, param))
throw std::runtime_error("ResourceHolder::load failed to load : "+file);
mResourceMap.insert(std::make_pair(id, std::move(resource)));
}
template<typename Resource>
Resource& Resources::ResourceHolder<Resource>::get(Resources::ID id)
{
auto found = mResourceMap.find(id);
return *found->second;
}
template<typename Resource>
const Resource& Resources::ResourceHolder<Resource>::get(Resources::ID id) const
{
auto found = mResourceMap.find(id);
return *found->second;
}
But when I try to compile it, I get these errors:
1> ResourceHolder.inl
1>d:\workspaces\resourceholder.inl(2): error C2143: syntax error : missing ';' before '<'
1>d:\workspaces\resourceholder.inl(2): error C2182: 'ResourceHolder' : illegal use of type 'void'
1>d:\workspaces\resourceholder.inl(2): error C2988: unrecognizable template declaration/definition
1>d:\workspaces\resourceholder.inl(2): error C2059: syntax error : '<'
1>d:\workspaces\resourceholder.inl(2): error C2039: 'load' : is not a member of '`global namespace''
1>d:\workspaces\resourceholder.inl(2): error C2653: 'Resources' : is not a class or namespace name
1>d:\workspaces\resourceholder.inl(3): error C2039: 'string' : is not a member of 'std'
1>d:\workspaces\resourceholder.inl(13): error C2039: 'load' : is not a member of '`global namespace''
1>d:\workspaces\resourceholder.inl(13): error C2653: 'Resources' : is not a class or namespace name
1>d:\workspaces\resourceholder.inl(14): error C2039: 'string' : is not a member of 'std'
1>d:\workspaces\resourceholder.inl(16): error C2143: syntax error : missing ';' before '{'
1>d:\workspaces\resourceholder.inl(16): error C2447: '{' : missing function header (old-style formal list?)
1>d:\workspaces\resourceholder.inl(24): error C2143: syntax error : missing ';' before '<'
1>d:\workspaces\resourceholder.inl(24): error C2040: 'ResourceHolder' : 'Resource &' differs in levels of indirection from 'int'
1>d:\workspaces\resourceholder.inl(24): error C2530: 'ResourceHolder' : references must be initialized
1>d:\workspaces\resourceholder.inl(24): error C2988: unrecognizable template declaration/definition
1>d:\workspaces\resourceholder.inl(24): error C2059: syntax error : '<'
1>d:\workspaces\resourceholder.inl(24): error C2039: 'get' : is not a member of '`global namespace''
1>d:\workspaces\resourceholder.inl(24): error C2653: 'Resources' : is not a class or namespace name
1>d:\workspaces\resourceholder.inl(31): error C2039: 'get' : is not a member of '`global namespace''
1>d:\workspaces\resourceholder.inl(31): error C2653: 'Resources' : is not a class or namespace name
1>d:\workspaces\resourceholder.inl(32): error C2143: syntax error : missing ';' before '{'
1>d:\workspaces\resourceholder.inl(32): error C2447: '{' : missing function header (old-style formal list?)
I've tried to figure it out for the past 2 hours, also I've tried eliminating the namespace.
What is the proper way of making a template class separated in a .h file and a .inl file?
EDIT:
This seems to be the proper way. The errors are there because VS studio saw my .inl file as a source file and tried to compile it. To get rid of that problem I removed it from my project and then added it again.