如何正确创建一个由两个文件分隔的模板类
How to properly create a template class separated in 2 files
我一直在读《SFML游戏开发》一书,我想创建一个模板类。我把它分为两个文件:一个.h文件和一个.inl文件,类似于
// 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;
}
但当我试图编译它时,我会遇到以下错误:
1> ResourceHolder.inl
1>d:workspacesresourceholder.inl(2): error C2143: syntax error : missing ';' before '<'
1>d:workspacesresourceholder.inl(2): error C2182: 'ResourceHolder' : illegal use of type 'void'
1>d:workspacesresourceholder.inl(2): error C2988: unrecognizable template declaration/definition
1>d:workspacesresourceholder.inl(2): error C2059: syntax error : '<'
1>d:workspacesresourceholder.inl(2): error C2039: 'load' : is not a member of '`global namespace''
1>d:workspacesresourceholder.inl(2): error C2653: 'Resources' : is not a class or namespace name
1>d:workspacesresourceholder.inl(3): error C2039: 'string' : is not a member of 'std'
1>d:workspacesresourceholder.inl(13): error C2039: 'load' : is not a member of '`global namespace''
1>d:workspacesresourceholder.inl(13): error C2653: 'Resources' : is not a class or namespace name
1>d:workspacesresourceholder.inl(14): error C2039: 'string' : is not a member of 'std'
1>d:workspacesresourceholder.inl(16): error C2143: syntax error : missing ';' before '{'
1>d:workspacesresourceholder.inl(16): error C2447: '{' : missing function header (old-style formal list?)
1>d:workspacesresourceholder.inl(24): error C2143: syntax error : missing ';' before '<'
1>d:workspacesresourceholder.inl(24): error C2040: 'ResourceHolder' : 'Resource &' differs in levels of indirection from 'int'
1>d:workspacesresourceholder.inl(24): error C2530: 'ResourceHolder' : references must be initialized
1>d:workspacesresourceholder.inl(24): error C2988: unrecognizable template declaration/definition
1>d:workspacesresourceholder.inl(24): error C2059: syntax error : '<'
1>d:workspacesresourceholder.inl(24): error C2039: 'get' : is not a member of '`global namespace''
1>d:workspacesresourceholder.inl(24): error C2653: 'Resources' : is not a class or namespace name
1>d:workspacesresourceholder.inl(31): error C2039: 'get' : is not a member of '`global namespace''
1>d:workspacesresourceholder.inl(31): error C2653: 'Resources' : is not a class or namespace name
1>d:workspacesresourceholder.inl(32): error C2143: syntax error : missing ';' before '{'
1>d:workspacesresourceholder.inl(32): error C2447: '{' : missing function header (old-style formal list?)
在过去的两个小时里,我一直在努力解决这个问题,也尝试删除名称空间。
在.h文件和.inl文件中分离模板类的正确方法是什么?
编辑:
这似乎是正确的方式。出现错误的原因是VS studio将我的.inl文件视为源文件,并试图对其进行编译。为了解决这个问题,我将其从项目中删除,然后再次添加。
我找到了答案。如果其他人有这个问题,是因为Visual Studio认为你的.inl文件是源文件,并试图编译它。
若要解决此问题,您需要将其从项目中删除,然后重新添加。如果在项目浏览器中显示的图标旁边有2个"+",则无法编译。这是源文件的图标。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 读取制表符分隔的文件时出现问题 C++
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 如何将逗号分隔的文件读取为 2D int 数组?
- 读取带有分隔C++的文件
- 嵌套结构,从由空行分隔的文件中读取数据
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- 如何从文件中读取用空格分隔的数字
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- C++将分隔文件导入矢量
- C++ 在关键字段上联接两个管道分隔文件
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- C++ 读取分隔文件并创建对象
- C++:读取分隔文件并存储在结构中
- 如何读取分隔文件的最后一行
- 如何在 Xcode 中分隔文件类
- visual c++6-用c++编辑管道分隔文件