Проблема с вложенным шаблоном класса C ++ - PullRequest
8 голосов
/ 25 августа 2011

У меня проблема с объявлением метода для вложенного шаблона класса.У меня есть что-то вроде этого:

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() {

Я не совсем уверен, что здесь делать, и мне было трудно найти подобные проблемы в Интернете.Любая помощь будет оценена.

1 Ответ

7 голосов
/ 25 августа 2011

Скажите это:

template <typename T>
template <typename U>
std::map<char, typename HashTrie<T>::template Node<U> > HashTrie<T>::Node<U>::foo()
{
}
...