在类模板中找不到声明的类型
Declared type not found in class template
如果您能帮助我解决以下错误,我将不胜感激。我得到了一个"在struct Frobnigator<Foo>
错误中找不到类型Attributes
",即使结构Frobigator声明了这样的成员,如下所示(另请参阅ideone.com)。
struct Foo{};
struct Bar{};
/////////////////////////////////////////////////////////////////////////////
template<typename P>
struct Attributes
{
};
template<>
struct Attributes<Foo>
{
};
/////////////////////////////////////////////////////////////////////////////
template<typename P>
struct Frobnigator
{
Attributes<P> attributes;
};
/////////////////////////////////////////////////////////////////////////////
template<typename P>
struct OuterHandler
{
typedef Frobnigator<P> Frob;
template<typename T>
struct InnerHandler;
void doStuff();
};
template<>
struct OuterHandler<Foo>
{
typedef Frobnigator<Foo> Frob;
template<typename T>
struct InnerHandler;
void doStuff();
};
/////////////////////////////////////////////////////////////////////////////
template<typename P>
template<typename T>
struct OuterHandler<P>::InnerHandler
{
typename T::Attributes attributes;
InnerHandler(){}
};
template<typename T>
struct OuterHandler<Foo>::InnerHandler
{
typename T::Attributes attributes;
InnerHandler(){}
};
/////////////////////////////////////////////////////////////////////////////
template<typename P>
void OuterHandler<P>::doStuff()
{
InnerHandler<Frob>();
}
void OuterHandler<Foo>::doStuff()
{
InnerHandler<Frob>();
}
/////////////////////////////////////////////////////////////////////////////
int main()
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
Visual studio错误消息
Test.cpp(62) : error C2039: 'Attributes' : is not a member of 'Frobnigator<P>'
with
[
P=Foo
]
Test.cpp(76) : see reference to class template instantiation 'OuterHandler<Foo>::InnerHandler<T>' being compiled
with
[
T=OuterHandler<Foo>::Frob
]
g++(GCC)4.5.3错误消息
Test.cpp: In instantiation of ‘OuterHandler<Foo>::InnerHandler<Frobnigator<Foo> >’:
Test.cpp:76:21: instantiated from here
Test.cpp:62:25: error: no type named ‘Attributes’ in ‘struct Frobnigator<Foo>’
什么令人困惑?Frobnigator<T>
实际上没有名为Attributes
的类型成员。
如果您使用的是C++11,请尝试:
typename decltype(T::attributes) attributes;
否则,可能更清楚:
template<typename P>
struct Frobnigator
{
typedef Attributes<P> attributes_type;
attributes_type attributes;
};
然后您可以在任何地方使用Frobnigator<T>::attributes_type
。
如果我理解得对,您正在寻找与Boost中的graph_traits<Graph>
类似的实现。
#include <iostream>
using namespace std;
struct Foo {};
template<typename T>
struct Attributes
{
};
template<>
struct Attributes<Foo>
{
};
template<typename T>
struct Frobnigator
{
typedef Attributes<T> Attr;
Attr attributes;
};
template<typename T>
struct traits
{
typedef typename T::Attr Attributes;
};
int main()
{
traits<Frobnigator<Foo> >::Attributes a;
return 0;
}
相关文章:
- std::enable_if 更改成员 *变量* 声明/类型
- 对前向声明类型进行unique_ptr的解决方法
- 如何将声明类型作为参数发送
- 在 Fortran 中泛化特定声明类型的操作
- 使用 (c++11) 声明类型时放置 __declspec(dllimport) 关键字的位置
- 模板函数可以使用带有删除的构造函数的声明类型
- 声明类型没有任何可变类型
- 如何使用声明类型简化此代码
- 使用声明类型选择函数专业化
- 替代班级内声明类型的别名
- 对于自动键入推论的参数,声明类型(自动)应推导的内容
- 使用 decltype 的条件声明类型
- 您如何声明类型结构的堆栈?在C 中
- 将指向前向声明类型的指针推送到 typedef'd 向量时出现编译器错误
- 声明适用于 auto,但不能显式声明类型?
- 声明类型包含未展开的参数包'Args'
- 课堂上的Typedef.在另一个类错误中:尚未声明类型
- 冲突的声明类型/价值不匹配
- 获取具有AST访问者clang的功能声明类型
- 变量声明类型定义 C 中的约定