简体   繁体   English

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

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

i am writing this code a generic list , now in this generic list i did a class for iterator that helds two parameters : one is a pointer to the list he points to . 我正在编写此代码的通用列表,现在在该通用列表中,我为迭代器做了一个类,它包含两个参数:一个是指向他指向的列表的指针。 and the other one points to the element in the list he points to .. 另一个指向他所指向的列表中的元素。

now i need to write an insert function that inserts a new element to a given list , with the following rules :: where i insert a new node to the list, and in this function if the iterator poiints to the end of the list the we add the new node to the end of the list else we insert the node one place before the node that the iterator currently points to , and if the iteratot points to a different node then thats an error . 现在,我需要编写一个insert函数,将一个新元素插入到给定列表中,并具有以下规则::在这里,我向列表中插入一个新节点,如果迭代器指向列表的末尾,则在此函数中,我们将新节点添加到列表的末尾,否则我们在迭代器当前指向的节点之前一个位置插入该节点,如果iteratot指向另一个节点,那就是错误。

now i want the iterator to be defoult so in case in the tests someone called the function with one parameter i want the iterator to be equal to the end element of the list. 现在我希望迭代器不存在,因此在测试中有人用一个参数调用了函数,我希望迭代器等于列表的结束元素。 the function end i wrote it inside of the list. 函数结束我将其写在列表中。 now in the insert function i did that but i get this error : 现在在插入功能,我做到了,但我得到这个错误:

cannot call member function "List::iterator List::end()"without object . 不能在没有对象的情况下调用成员函数“ 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;
};

any help would be amazing ! 任何帮助都将是惊人的! cause i don't know why such a mistake apears . 因为我不知道为什么这样的错误会消失。

//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());

This is the offending line. 这是违规行。 A default argument cannot be the result of a member function call. 默认参数不能是成员函数调用的结果。 The right tool to achieve what you want is overloading. 实现您想要的正确工具是过载。

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

Here we still defer to the insert function you implemented, but we call end from within the overloads body, where it's allowed. 在这里,我们仍然遵循您实现的insert函数,但是我们在允许重载的地方调用end

Don't worry about the indirect call. 不用担心间接调用。 This is a very small function that's defined inside the class declaration itself. 这是一个非常小的函数,它在类声明本身内部定义。 Any decent compiler will inline it completely. 任何体面的编译器都会完全内联它。

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

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