[英]Declared type not found in class template
I'd appreciate help resolving the following error. 感谢您帮助解决以下错误。 I'm getting a 'type
Attributes
not found in struct Frobnigator<Foo>
error' even though struct Frobnigator declares such a member, as shown below (see also ideone.com ). 即使struct Frobnigator声明了这样的成员,我也得到了“在
struct Frobnigator<Foo>
错误中找不到的类型Attributes
”,如下所示(另请参见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 error message 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 error message 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>’
What's confusing? 什么令人困惑?
Frobnigator<T>
indeed has no type member named Attributes
. Frobnigator<T>
确实没有名为Attributes
类型成员。
If you're using C++11, try: 如果您使用的是C ++ 11,请尝试:
typename decltype(T::attributes) attributes;
Otherwise, and probably clearer anyway: 否则,可能更清晰:
template<typename P>
struct Frobnigator
{
typedef Attributes<P> attributes_type;
attributes_type attributes;
};
Then you can use Frobnigator<T>::attributes_type
anywhere. 然后,您可以在任何地方使用
Frobnigator<T>::attributes_type
。
If I understand it right, your are looking for something similar implementation as graph_traits<Graph>
in Boost. 如果我理解正确,那么您正在寻找与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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.