2015-11-30 58 views
2

我试图让它编译,但遇到了嵌套类的问题。具有嵌套类的可变长度参数模板类

struct TKey { 
    char a[2]; 
}; 

template<class TKEY, class TOBJ> 
struct CHash { 
    struct TNode { 
     TKEY Key; 
     TOBJ Obj; 
    }; 
    int stuff; 
}; 

template <class TKEY, class... ARGS> 
class CNested : public CHash<TKEY, int> { 
public: 
    typedef CNested<ARGS...>   TNested; 

    template<class TKEY1, class... ARGS1> 
    struct TNodeType { 
     typedef typename TNested::TNodeType<ARGS1...>::Type Type; 
    }; 

    template<class TKEY> 
    struct TNodeType<TKEY> { 
     typedef TNode Type; 
    }; 

    CNested() { } 

}; 

// recursive template, tail 
template <class TKEY, class TOBJ> 
class CNested<TKEY, TOBJ> : public CHash<TKEY, TOBJ> { 
public: 
    CNested() { } 
}; 


int main(int argc, char* argv[]) 
{ 
    // p should have type of CNested<TKey, TKey, int>::TNode* 
    CNested<TKey, TKey, TKey, int>::TNodeType<TKey>* p; 
    return 0; 
} 
+0

可能的重复[哪里和为什么我必须把“模板”和“typename”关键字?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – SirGuy

回答

3

TNodeType是依赖模板的名称,因此,你需要:

嵌套结构
typedef typename TNested::template TNodeType<ARGS1...>::Type Type; 
          ^^^^^^^^ 

同样的外class CNestedTNodeType参数TKEY阴影参数TKEY,所以你需要更改为:

template<class TKEY1> 
struct TNodeType<TKEY1> { 
    typedef TNode Type; 
}; 
+0

谢谢!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! – johnnycrash

+0

哦,是的......那TKEY是一个错字......从我不得不削减我的代码来问这个问题。 – johnnycrash