无效使用不完整类型模板构造函数声明

时间:2013-04-15 15:07:38

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

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

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

2 个答案:

答案 0 :(得分: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)

顺便说一句,如果(并且仅当)您需要在其他编译单元you should define it in the header中访问此模板化构造函数。

答案 1 :(得分: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)