我遇到嵌套类模板的方法声明问题。 我有这样的事情:
template <typename T>
class HashTrie
{
template <typename Y>
class Entry
{ // members and methods here
};
template <typename U>
class Node
{ // members and methods here
};
// members and methods here
}
以下似乎没有问题:
template <typename T>
template <typename Y>
HashTrie<T>::Entry<Y> HashTrie<T>::Entry<Y>::someMethod() {
//...
}
然而,这不是:
template <typename T>
template <typename U>
std::map<char, HashTrie<T>::Node<U> > HashTrie<T>::Node<U>::anotherMethod() {
// ...
}
我在GCC上收到以下错误
./HashTrie.h:389: error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
./HashTrie.h:389: error: expected a type, got ‘(HashTrie::Node < <expression error>)’
./HashTrie.h:389: error: template argument 4 is invalid
./HashTrie.h:389: error: expected unqualified-id before ‘>’ token
我尝试添加一个类型名称,但这似乎没有帮助
template <typename T>
template <typename U>
std::map<char, typename HashTrie<T>::Node<U> > HashTrie<T>::Node<U>::anotherMethod() {
// ...
}
导致......
./HashTrie.h:389: error: template argument 2 is invalid
./HashTrie.h:389: error: template argument 4 is invalid
./HashTrie.h:389: error: expected unqualified-id before ‘>’ token
icpc说:
./HashTrie.h(389): error: template parameter "HashTrie<T>::Node [with T=T]" may not have a template argument list
std::map<char, typename HashTrie<T>::Node<U> > HashTrie<T>::Node<U>::newNodeMap() {
我不确定该怎么做,并且很难在网上找到任何类似的问题。任何帮助将不胜感激。
答案 0 :(得分:7)
说出来:
template <typename T>
template <typename U>
std::map<char, typename HashTrie<T>::template Node<U> > HashTrie<T>::Node<U>::foo()
{
}