footy
footy

Reputation: 5911

syntax error while returning nested class of a template

I have the following class declaration:

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
};

When I am declaring the SkipList insert function outside the class definition

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)

the following error comes:

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

What is wrong with my code?

Upvotes: 0

Views: 143

Answers (1)

Matt Phillips
Matt Phillips

Reputation: 9691

You need the typename keyword:

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

Or else the compiler thinks Iterator is a class member.

Upvotes: 3

Related Questions