[英]invalid use of incomplete type template constructor declaraion
下面是我的模板类。 为什么会发生错误?
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>’
您的类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中对其进行定义 。
您缺少一个参数,只有两个,但是需要三个,这可行:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.