所以我有SkipList.hpp,它有一个名为SkipListIterator的嵌套模板类。
//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
^
答案 0 :(得分: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;
};
使用模板时,编译器通常需要额外的帮助来确定它的类型,模板或非类型。