-1

我正在尝试编写与标准库兼容的链表集,但遇到了我无法理解的错误。

#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,但我不明白我的代码所做的与那里定义的类型有什么关系。

4

1 回答 1

1

您将迭代器中的所有类型声明为私有类型定义,因此它们在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了 algo with 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;
于 2018-12-04T09:13:10.183 回答