2017-03-07 139 views
2

因此,我有SkipList.hpp,它有一個名爲SkipListIterator的嵌套模板類。在模板類中使用模板化嵌套類的Typedef

//SkipList.hpp 
template <class Key_t, class Mapped_t> 
class SkipList { 
    template <bool isConst, bool isReverse> 
    class SkipListIterator { 
     ... 
    } 
    ... 
} 

在我的Map.hpp中,我想爲不同類型的迭代器進行typedef。我試圖做的是以下幾點:

//Map.hpp 

#include "SkipList.hpp" 
template <class Key_t, class Mapped_t> 
class Map { 
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, false> iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<true, false> const_iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, true> reverse_iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<true, true> const_reverse_iterator; 
    ... 
} 

這不起作用,和g ++給了我以下錯誤:

error: non-template 'SkipListIterator' used as template 
typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, false> iterator 
              ^

回答

2

這個工程用gcc 6.3.1:

template <class Key_t, class Mapped_t> 
class SkipList { 
    template <bool isConst, bool isReverse> 
    class SkipListIterator { 
    }; 
}; 

template <class Key_t, class Mapped_t> 
class Map { 
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<false, false> iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<true, false> const_iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<false, true> reverse_iterator; 
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<true, true> const_reverse_iterator; 
}; 

使用模板時,編譯器通常需要額外的幫助來確定它是類型,模板還是非類型。

+0

感謝您的快速答案,完美工作! – Adam