我正在尝试编写与标准库兼容的链表集,并且遇到我无法理解的错误。
#include <iterator>
#include <cstddef>
template <typename T>
class SetList{
struct ListNode{
T data;
ListNode * next;
ListNode(T newData, ListNode* newNext): data(newData), next(newNext){}
ListNode(ListNode& l): data(l.data), next(l.next){}
ListNode& operator=(ListNode & l){
data = l.data;
next = l.next;
return this;
}
};
public:
ListNode* head;
typedef T value_type;
class iterator;
typedef iterator iterator_type;
class const_iterator;
SetList(): head(nullptr){}
~SetList(){
//delete entire list
}
SetList(SetList<T>& sl): head(sl.head){}
iterator begin(){
return iterator(head);
}
iterator end(){
return iterator(nullptr);
}
class iterator
{
//traits
friend class SetList;
friend class const_iterator;
typedef std::forward_iterator_tag iterator_category;
typedef iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef int difference_type;
private:
ListNode* ihead;
public:
iterator(ListNode* newHead = nullptr): ihead(newHead){}
iterator(const iterator& it): ihead(it.ihead){}
iterator& operator=(iterator& it){ihead = it.ihead; return *this;}
reference operator*(){return ihead->data;}
pointer operator->() const{ return ihead;}
iterator& operator++(){ihead = ihead->next; return *this;}
bool operator!=(const iterator& it) const{
return ihead != it.ihead;
}
bool operator==(const iterator& it) const{
return ihead == it.ihead;
}
};
class const_iterator
{
//traits
friend class SetList;
friend class iterator;
typedef std::forward_iterator_tag iterator_category;
typedef const_iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef int difference_type;
private:
const ListNode* ihead;
public:
const_iterator(const ListNode* newHead = nullptr): ihead(newHead){}
const_iterator(const iterator& it): ihead(it.ihead){}
const_iterator(const const_iterator& it): ihead(it.ihead){}
const_iterator& operator=(const const_iterator& it){ihead = it.ihead; return *this;}
const_iterator& operator=(const iterator& it){ihead = it.ihead; return *this;}
reference operator*()const {return ihead->data;}
pointer operator->() const{ return ihead;}
const_iterator& operator++(){ihead = ihead->next; return *this;}
bool operator!=(const const_iterator& it) const{
return ihead != it.ihead;
}
bool operator==(const const_iterator& it) const{
return ihead == it.ihead;
}
};
public:
void insert(T newData){
if(head){
ListNode* cur = new ListNode(newData, head);
head = cur;
}else{
head = new ListNode(newData, nullptr);
}
}
SetList& operator=(SetList& sl){
}
};
这似乎是在说iterator_traits
中找不到某些类型,但是我不明白我的代码的工作方式与那里定义的类型有关。
答案 0 :(得分:1)
您在迭代器中将所有类型都声明为私有 typedefs ,因此它们无法从iterator_traits
使用。如果您更改:
friend class SetList;
friend class const_iterator;
typedef std::forward_iterator_tag iterator_category;
typedef iterator self_type;
typedef T value_type;
收件人:
friend class SetList;
friend class const_iterator;
public:
typedef std::forward_iterator_tag iterator_category;
typedef iterator self_type;
typedef T value_type;
//...
代码编译。编译器错误表明您已将std::copy
算法和SetList
一起使用,下面是在迭代器中将typedef设置为public后可以正常工作的代码:
SetList<int> sl;
sl.insert(1);
std::vector<int> v;
std::copy(sl.begin(), sl.end(), std::back_inserter(v));
for (int i : v)
std::cout << i << std::endl;