inl实现编译模板类

inl实现编译模板类

本文介绍了Visual studio不会用* .inl实现编译模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注一本关于SFML游戏开发的书,但我坚持在第二章,因为我无法编译我刚才写的代码。



它几乎是从书中逐字复制(除了成员变量名和异常文本)。我有C ++和模板的经验,但我从来没有看到这个错误,我一直盯着这个几个小时现在,我没有看到任何错误的代码。



这是我的* .h文件:

  #pragma once 
#include< map&
#include< memory>
#include< string>
#include< stdexcept>
#include< cassert>
#includeenumFile.h

template< typename Resource,typename标识符>
class ResourceHolder
{
public:
ResourceHolder(void);
〜ResourceHolder(void);

void load(Identifier id,const std :: string& filename);

template< typename参数>
void load(Identifier id,const std :: string& filename,
const Parameter& secondParam);

资源& get(Identifier id);
const Resource& get(Identifier id)const;

private:
std :: map< Identifier,std :: unique_ptr< Resource> resourceMap;
};

#includeResourceHolder.inl

这里是my *。 inl file:

  template< typename Resource,typename Identifier> 
void ResourceHolder< Resource,Identifier> :: load(Identifier id,const std :: string& filename)
{
//创建和加载资源
std :: unique_ptr<资源> resource(new Resource());
if(!resource-> loadFromFile(filename))
throw std :: runtime_error(加载资源失败+ filename);

//如果加载成功,插入资源映射
auto inserted = resourceMap.insert(std :: make_pair(id,std :: move(resource)));
assert(inserted.second);
}

template< typename Resource,typename标识符>
template< typename参数>
void ResourceHolder< Resource,Identifier> :: load(Identifier id,const std :: string& filename,
const Parameter& secondParam)
{
//创建并加载资源
std :: unique_ptr< Resource> resource(new Resource());
if(!resource-> loadFromFile(filename,secondParam))
throw std :: runtime_error(无法加载资源:+ filename);

//如果加载成功,插入资源映射
auto inserted = resourceMap.insert(std :: make_pair(id,std :: move(resource)));
assert(inserted.second);
}

template< typename Resource,typename标识符>
资源& ResourceHolder< Resource,Identifier> :: get(Identifier id)
{
auto found = resourceMap.find(id);
assert(found!= resourceMap.end());

return * found-> second;
}

template< typename Resource,typename标识符>
const Resource& ResourceHolder< Resource,Identifier> :: get(Identifier id)const
{
auto found = resourceMap.find(id);
assert(found!= resourceMap.end());

return * found-> second;
}

对不起,很多代码,但我绝望在这里。我得到不寻常的错误,所有在* .inl文件,而不是写它们,我拍了截图:



有什么想法如何解决这个问题?



编辑:
















$ b b

 命名空间纹理
{
枚举ID {Landscape,Airplane,Missile};
}



当我想使用类时,我使用它如下: p>

  ResourceHolder< sf :: Texture,Textures :: ID>纹理
textureHolder.load(Textures :: Airplane,path / to / texture.png);


解决方案

从项目中删除或排除ResourceHolder.inl文件在VS然后添加它再次解决了我的问题。我不知道为什么这个工作,但现在它编译好。


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");
解决方案

Removing or excluding the ResourceHolder.inl file from the project in VS and then adding it again solved the problem for me. I have no idea as to why this worked, but now it compiles fine.

这篇关于Visual studio不会用* .inl实现编译模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:16