0

Below is my template class. Why is the error occuring?

template <typename Key_T, typename Mapped_T, size_t MaxLevel = 5>
class SkipList
{
    typedef std::pair<Key_T, Mapped_T> ValueType;
public:

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

    size_t size() const;
    Iterator<Key_T,Mapped_T> begin();
    Iterator<Key_T,Mapped_T> end();
    //ConstIterator begin() const;
    //ConstIterator end() const;
    virtual void clear();


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

    virtual void erase(Iterator<Key_T,Mapped_T> pos);
    virtual void erase(Iterator<Key_T,Mapped_T> range_beg, Iterator<Key_T,Mapped_T> range_end);

private:
    Iterator<Key_T,Mapped_T>* head;
    Iterator<Key_T,Mapped_T>* tail;
    float probability;
    size_t maxHeight;
    size_t curHeight;
    RandomHeight* randGen;
};

template <typename Key_T1,typename Mapped_T1,typename Key_T2,typename Mapped_T2>
bool operator==(const SkipList<Key_T1,Mapped_T1> &a, const SkipList<Key_T2,Mapped_T2> &b);
template <typename Key_T1,typename Mapped_T1,typename Key_T2,typename Mapped_T2>
bool operator!=(const SkipList<Key_T1,Mapped_T1> &a, const SkipList<Key_T2,Mapped_T2> &b);
template <typename Key_T1,typename Mapped_T1,typename Key_T2,typename Mapped_T2>
bool operator<(const SkipList<Key_T1,Mapped_T1> &a, const SkipList<Key_T2,Mapped_T2> &b);

template <typename Key_T, typename Mapped_T>
SkipList<Key_T,Mapped_T>::SkipList() : curHeight (1), maxHeight(MaxLevel) , probability (1.0/MaxLevel)
{
  randGen = new RandomHeight(MaxLevel,probability);

  // Create head and tail and attach them
  head = new Iterator<Key_T,Mapped_T> (maxHeight);
  tail = new Iterator<Key_T,Mapped_T> (maxHeight);
  head->fwdNodes = tail;
}

Error:

SkipList.cpp:134:36: error: invalid use of incomplete type ‘class SkipList<Key_T, Mapped_T>’
SkipList.cpp:93:7: error: declaration of ‘class SkipList<Key_T, Mapped_T>’
footy
  • 5,803
  • 13
  • 48
  • 96
  • I have already declared and defined the `Iterator` template above this. – footy Apr 15 '13 at 15:08
  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – juanchopanza Apr 15 '13 at 15:12

2 Answers2

2

Your class SkipList has three template parameters.

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

You must account for that here.

// I've added "size_t MaxLevel"
template <typename Key_T, typename Mapped_T, size_t MaxLevel>
SkipList<Key_T,Mapped_T,MaxLevel>::SkipList() : curHeight (1), maxHeight(MaxLevel) , probability (1.0/MaxLevel)

As an aside, if (and only if) you need to access this templated constructor in other compilation units, you should define it in the header.

Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
2

You are missing a parameter, you only have two but you need three, this works:

template <typename Key_T, typename Mapped_T, size_t MaxLevel>
SkipList<Key_T,Mapped_T,MaxLevel>::SkipList() : curHeight (1), maxHeight(MaxLevel) , probability (1.0/MaxLevel)
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740