Попытка в контейнере STL - PullRequest
0 голосов
/ 03 декабря 2018

Я пытаюсь создать версию std :: set, используя связанный список.Я думаю, что я реализовал это в основном правильно, но я получаю ошибку компиляции, которую я не могу расшифровать.Я был бы признателен, если бы кто-нибудь заметил ошибку в моем коде и объяснил, как мне отследить такую ​​ошибку.Это означает, что ошибка связана с функциями stl.

#include <iterator>
#include <cstddef>
template <typename Type>
struct ListNode{
        Type info;
        ListNode<Type> * next;
        ListNode(Type newInfo, ListNode<Type> * newNext) : info(newInfo), next(newNext){
            }
        ListNode(ListNode<Type>& L): info(L.info), next(L.next){
        }
        ListNode<Type>& operator=(ListNode<Type>& L){
            info = L->info;
            next = L->next;
            return this;
        }
        };
template <typename Type>
class SetList{
    ListNode<Type> * head;
    ListNode<Type> * tail;
       public:
        typedef ListNode<Type> value_type;
        SetList() : head(nullptr), tail(nullptr){
        }
        SetList(SetList & s){

        }
        ~SetList(){
            //ListNode<Type> * cur = head;
            //ListNode<Type> * next = cur;
            //while(cur){
        //      next = cur->next;
        //      delete cur;
        //      cur = next;
          //  }
        }


        struct iterator{
            //traits
            typedef std::forward_iterator_tag iterator_category;
            typedef iterator self_type;
            typedef Type value_type;
            typedef Type& reference;
            typedef Type* pointer;
            typedef ptrdiff_t difference_type;
            private:
                //rename to ihead
                ListNode<Type>* ibuf;
            public:
                iterator(ListNode<value_type>* node) : ibuf(node){}
                self_type& operator++(){ibuf = ibuf->next; return *this;}
                self_type operator++(int postfix){
                   self_type cpy = *this;
                   ibuf = ibuf->next;
                   return cpy;
                }
                reference operator*(){return ibuf->info;}
                pointer operator->(){return &ibuf->info;}
                self_type operator=(const iterator& it){insert(*it);}
  bool operator==(const self_type& rhs) const {return ibuf->info == rhs.ibuf->info;}
                bool operator !=(const self_type& rhs) const {return ibuf->info != rhs.ibuf->info;}
        };

        iterator begin(){ return iterator(head);}
        iterator end() { return iterator(nullptr);}
//      const_iterator begin() { return const_iterator(head);}
//      const_iterator end() { return const_iterator(tail);}
        Type operator[](int index){
            iterator cur(head);
            for(int i = 0; i < index; ++i,++cur){
            }
            return *cur;
        }
        SetList<Type>& operator=(const SetList<Type>& s){
            head = s.head;
            tail = s.tail;
            return this;
        }
        iterator find(Type toFind){
            ListNode<Type> * cur = head;
            while(cur){
                if(cur->info == toFind)
                    return iterator(cur);
            }
            return this->end();
        }
        void insert(Type toInsert){
            ListNode<Type>* cur = nullptr;
            if(head){
                cur = new ListNode<Type>(toInsert, head);
                head = cur;
            }else{
                cur = new ListNode<Type>(toInsert, nullptr);
                head = cur;
            }

        }
};

Я вызываю копию в другом месте на моем наборе, мой вызов копирования работает с std :: set, но не с моим набором.Ошибка, которую я получаю, заключается в следующем.error

Надеюсь, это не слишком много, чтобы спросить.Вам даже не нужно читать мой код, даже просто узнать, как отследить большие ошибки, как это, было бы очень полезно.

1 Ответ

0 голосов
/ 03 декабря 2018

SetList<Type> должно иметь Type в качестве value_type, а не ListNode<Type>.

...