繁体   English   中英

如何将默认迭代器写入通用列表?

[英]how to write a default iterator to a generic list?

我正在编写此代码的通用列表,现在在该通用列表中,我为迭代器做了一个类,它包含两个参数:一个是指向他指向的列表的指针。 另一个指向他所指向的列表中的元素。

现在,我需要编写一个insert函数,将一个新元素插入到给定列表中,并具有以下规则::在这里,我向列表中插入一个新节点,如果迭代器指向列表的末尾,则在此函数中,我们将新节点添加到列表的末尾,否则我们在迭代器当前指向的节点之前一个位置插入该节点,如果iteratot指向另一个节点,那就是错误。

现在我希望迭代器不存在,因此在测试中有人用一个参数调用了函数,我希望迭代器等于列表的结束元素。 函数结束我将其写在列表中。 现在在插入功能,我做到了,但我得到这个错误:

不能在没有对象的情况下调用成员函数“ List :: iterator List :: end()”。

#include <iostream>
#include <assert.h>
#include "Exceptions.h"

template <class T>
class List {

public:
    List();
    List(const List&);
    ~List();
    List<T>& operator=(const List& list);
    template <class E>
    class ListNode {
        private:
             ListNode(const E& t, ListNode<E> *next): data(new E(t)), next(next){}
             ~ListNode(){delete data;}
             E* data;
             ListNode<E> *next;
        public:
            friend class List<E>;
            friend class Iterator;
            E getData() const{
               return *(this->data);
            }
            ListNode<E>* getNext() const{
               return this->next;
            }
            };
    class Iterator {
        const List<T>* list;
        int index;
        ListNode<T>* current;
        Iterator(const List<T>* list, int index): list(list),
                index(index),current(NULL){
            int cnt=index;
            while (cnt > 0) {
                current = current->next;
                cnt--;
                    }
        }
        friend class List<T>;
        friend class ListNode<T>;
    public:
       // more functions for iterator
    };
    Iterator begin() const ;
    Iterator end() const;
    void insert(const T& data, Iterator iterator=end());//here i get the error
    void remove(Iterator iterator);
    class Predicate{
    private:
      T target;
    public:
      Predicate(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i == target;
      }
    };
    Iterator find(const Predicate& predicate);
    class Compare{
    private:
      T target;
    public:
      Compare(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i < target;
      }
    };
    bool empty() const;
    int compareLinkedList(ListNode<T> *node1,  ListNode<T> *node2);

private:
   ListNode<T> *head;
   ListNode<T> *tail;
int size;
};

任何帮助都将是惊人的! 因为我不知道为什么这样的错误会消失。

//insert function just in case : 


template <typename T>
void List<T>::insert(const T& data, Iterator iterator=end()){
    if(iterator.list!=this){
        throw mtm::ListExceptions::ElementNotFound();
        return;
    }
    ListNode<T> *newNode = new ListNode<T>(data, iterator.current);
    if(iterator.index==size){
        if (head == NULL) {
            head = newNode;
            tail=newNode;
            }else{
        Iterator temp(this,size-1);
        temp.current->next=newNode;
            }
        //newNode->next=this->end().current;
           }
    else {
    if (head == NULL) {
    head = newNode;
    tail=newNode;
    }
   else {
       Iterator temp1(this,iterator.index-1);
        temp1.current->next=newNode;
        newNode->next=iterator.current->next;
   }

   }
    size++;
}
void insert(const T& data, Iterator iterator=end());

这是违规行。 默认参数不能是成员函数调用的结果。 实现您想要的正确工具是过载。

void insert(const T& data, Iterator iterator);
void insert(const T& data) { insert(data, end()); }

在这里,我们仍然遵循您实现的insert函数,但是我们在允许重载的地方调用end

不用担心间接调用。 这是一个非常小的函数,它在类声明本身内部定义。 任何体面的编译器都会完全内联它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM