0

下面是我的模板类。为什么会发生错误?

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

错误:

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>’
4

2 回答 2

2

您的类SkipList具有三个模板参数。

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

您必须在此处说明这一点。

// 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)

顺便说一句,如果(且仅当)您需要在其他编译单元中访问此模板化构造函数,您应该在 header 中定义它

于 2013-04-15T15:15:58.010 回答
2

你缺少一个参数,你只有两个,但你需要三个,这行得通:

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)
于 2013-04-15T15:16:18.510 回答