容器类中的模板类型
我编写了一个“Effect”类(用于 opengl 程序),并且我还尝试为其编写一个容器类。
Effect 类定义如下:
template <typename T>
class Effect
{
private:
Vbo<T> m_Vbo;
};
其中 T 是描述顶点属性的类型。
为了编写一个容器类,我想将这些效果存储在 std::map 中:
class EffectMgr : public Singleton <EffectMgr>
{
private:
typedef std::map<std::string, Effect<T> & > EffectMap;
};
我在容器类中遇到的错误是 T 未定义。有人可以启发我吗?
我可能(纯粹是偶然和修补)找到了答案,尽管在编写容器类之前我不会知道:
class EffectMgr : public Singleton <EffectMgr>,
{
private:
template <typename T>
typedef std::map<std::string, Effect<T> & > EffectMap;
};
I writing an "Effect" class (for an opengl program) and I'm also attempting to write a container class for.
The Effect class is defined as follows:
template <typename T>
class Effect
{
private:
Vbo<T> m_Vbo;
};
Where T is a type that describes the vertex attributes.
In order to write a container class, I'd like to store these effects in an std::map:
class EffectMgr : public Singleton <EffectMgr>
{
private:
typedef std::map<std::string, Effect<T> & > EffectMap;
};
The error I get with the container class is that T is undefined. Can someone enlighten me?
I may have (by sheer chance and tinkering) have found the answer although I won't know until I've written the container class:
class EffectMgr : public Singleton <EffectMgr>,
{
private:
template <typename T>
typedef std::map<std::string, Effect<T> & > EffectMap;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
T
的范围在 Effect 定义内。在范围之外,T
是未定义的。也许你的意思是这个?
如果您只想对 typedef 进行模板化,请执行以下操作:
T
is scoped within the Effect definition. Outside of the scopeT
is undefined.Perhaps you mean this?
If you want only the typedef to be templated then do this:
由于
Effect
是一个模板类,并且您没有在EffectMgr
中对其进行专门化,因此它也需要是一个模板:Since
Effect
is a template class, and you're not specializing it insideEffectMgr
, it also needs to be a template:据我了解,您希望在地图中存储具有不同
T
类型的效果。如果是,最简单的方法是指定接口
并在模板中实现它:
现在,您可以创建
std::map
通过额外的努力,您可以在
IEffect
上编写一个包装器以摆脱指针。As I understand, you want to store effects with a different
T
-ypes in a map.If it is, the simplest way would be specify interface
and implement it in your template:
now, you can create
std::map<std::string, IEffect* >
With additional effort, you can write a wrapper over
IEffect
to get rid of pointers.