gpt4 book ai didi

c++ - 如何正确创建一个分为两个文件的模板类

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:46 25 4
gpt4 key购买 nike

我一直在阅读 SFML 游戏开发一书,我想创建一个模板类。我将它分成 2 个文件:一个 .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:\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?)

在过去的 2 个小时里,我一直在努力解决这个问题,我也曾尝试删除命名空间。

将模板类分隔在 .h 文件和 .inl 文件中的正确方法是什么?

编辑:

这似乎是正确的方法。出现错误是因为 VS studio 将我的 .inl 文件视为源文件并尝试对其进行编译。为了摆脱这个问题,我将它从我的项目中删除,然后再次添加。

最佳答案

我找到了答案。如果其他人遇到此问题是因为 Visual Studio 认为您的 .inl 文件是源文件并尝试编译它。

要解决这个问题,您需要将其从您的项目中移除并重新添加。如果在您的项目浏览器中显示的图标旁边有 2 个“+”,则无法编译。那是源文件的图标。

关于c++ - 如何正确创建一个分为两个文件的模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20194324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com