I am trying to write a linked list set compatible with the standard-library and am getting an error I cannot understand.
#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){
}
};
This appears to be saying that it cannot find certain types in iterator_traits
, but I do not understand how what my code does has any bearing on what types are defined there.
You declared all types in iterator as private typedefs so they are unavailable from
iterator_traits
. If you change:to:
code compiles. Compiler errors tell that you have used
std::copy
algo withSetList
, below is the code which works fine after making typedefs in iterator as public: