返回模板的嵌套类时出现语法错误

时间:2013-04-20 00:35:57

标签: c++ templates c++11 g++ syntax-error

我有以下类声明:

template <typename Key_T, typename Mapped_T, size_t MaxLevel = 5>
class SkipList
{

public:

  class Iterator
  {
    typedef std::pair<Key_T, Mapped_T> ValueType;
    template <typename Key1, typename Obj1, size_t MaxLevel1> friend class SkipList;
    public:
      //Iterator functions

    private:
      //Iterator Data
  };

  SkipList();
  ~SkipList();
  SkipList(const SkipList &);
  SkipList &operator=(const SkipList &);

  std::pair<Iterator, bool> insert(const ValueType &);
  template <typename IT_T>
  void insert(IT_T range_beg, IT_T range_end);

  void erase(Iterator pos);


private:
  //data
};

当我在类定义之外声明SkipList insert函数时

template <typename Key_T, typename Mapped_T, size_t MaxLevel>
typename std::pair<SkipList<Key_T,Mapped_T,MaxLevel>::Iterator, bool>  SkipList<Key_T,Mapped_T,MaxLevel>::insert(const ValueType &input)

出现以下错误:

SkipList.cpp:349:69: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _T1, class _T2> struct std::pair’
SkipList.cpp:349:69: error:   expected a type, got ‘SkipList<Key_T, Mapped_T, MaxLevel>::Iterator’
SkipList.cpp:349:72: error: ‘SkipList’ in namespace ‘std’ does not name a type
SkipList.cpp:349:80: error: expected unqualified-id before ‘<’ token

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:3)

您需要typename关键字:

typename std::pair<typename SkipList<Key_T,Mapped_T,MaxLevel>::Iterator, bool>  SkipList<Key_T,Mapped_T,MaxLevel>::insert(const ValueType &input)

否则编译器认为Iterator是类成员。

相关问题