简体   繁体   English

在类模板中找不到声明的类型

[英]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.

相关问题 类模板实例化错误:未在此范围内声明类型 - Class template instantiation error: type not declared in this scope 为什么即使模板类型被声明为“ class”,仍需要“ typename”? - Why is `typename` still necessary even the template type is declared as `class`? 为什么c ++模板参数应该声明为类类型? - Why c++ template parameters should be declared as class type? 引用模板类中声明的结构 - Referencing a struct declared in a template class 未在基类模板中声明成员 - Member was not declared in base class template 带有前向声明类的模板函数 - Template function with a forward declared class extern声明模板专用功能未找到 - extern declared template specialized function not found 我可以使用本地声明的枚举类作为模板非类型参数吗? (gcc给出了不明确的错误) - Can I use a locally declared enum class as template non-type parameter? (gcc gives obscure error) 使用前向声明类的嵌套(尚未定义)类型的部分模板特化 - Partial template specialization using nested (undefined yet) type of a forward-declared class 是否可以使用decltype来确定前向声明的模板类的成员函数的返回类型? - Is it possible to use decltype to figure out the return type of a member function of a forward declared template class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM