简体   繁体   English

为链表重载 operator+

[英]Overloading operator+ for linked lists

I'm a beginner and now I am trying to implement the class linked list which contains the function begin().我是初学者,现在我正在尝试实现包含 function begin() 的 class 链表。 The function return well the first element in the list, but what i am trying to do is to return the iterator on the next position, for instance something like this: function 很好地返回了列表中的第一个元素,但我想做的是在下一个 position 上返回迭代器,例如像这样:

List<int>::iterator iter2 = a.begin() + 2; // or iter2 = iter2 + 1;
cout <<iter2->data;

Where the output is garbage like 21213123.. output 是像 21213123 这样的垃圾。

So here I was thinking I should use an operator overloading+, here is my function:所以在这里我想我应该使用运算符重载+,这是我的 function:

template<class T>
Node<T>* operator+(const Node<T>& iter, const int& pos)
{
    cout << "in"; for testing, but seems that doesnt even entry here

    return NULL;
}

So can anyone help me?那么任何人都可以帮助我吗? Thank you very much非常感谢

PS: Here is the class Node PS:这里是 class 节点

 template<class T>
class Node {
public:
    T data;
    Node* next;
    Node() :data(0), next(NULL) {}
    Node(T val, Node<T>* pointer = NULL) :data(val), next(pointer) {}

};

and list class并列出 class

template<class T>
class List {


public:
    typedef Node<T>* iterator;
    typedef const Node<T>* const_iterator;
    //constructors
    List() { item = NULL; counter = 0; }
    explicit List(int val) :counter(1) { item = new Node<T>(val); }
    ~List() { // to be made 
    }
    //public functions
    int size() { return counter; }

    iterator begin() {
        return item;
    }
    iterator end()
    {
        iterator last = item;
        while (last->next != NULL)
        {
            last = last->next;
        }
        return last;

    }

    void push_front(const int& val) {
        iterator newNode = new Node<T>(val, item);
        item = newNode;

        counter++;
    }
    void append(const int& val)
    {
        iterator newnode = new Node<T>(val);
        newnode->next = NULL;
        iterator last = item;
        if (item == NULL)
        {
            item = newnode;
            return;
        }
        while (last->next != NULL)
            last = last->next;

        last->next = newnode;

        counter++;
    }

    int operator[](const int&);

private:

    iterator item;
    int counter;
};

Let's take a look at your begin function:让我们来看看你的begin function:

typedef Node<T>* iterator;
iterator begin() {
    ...
}

This function returns a Node<T>* , a pointer to a Node<T> object.这个 function 返回一个Node<T>* ,一个指向Node<T> object 的指针。 As a result, when you write结果,当你写

list.begin() + 2;

C++ interprets this to mean "I've got a pointer, and I've got a number, so I'll step that pointer forward the appropriate number of steps." C++ 将其解释为“我有一个指针,我有一个数字,所以我会将该指针向前推进适当的步数。”

You're then asking - well, wait a minute, why isn't this overloaded operator getting called?然后你会问 - 好吧,等一下,为什么不调用这个重载的运算符?

template<class T>
Node<T>* operator+(const Node<T>& iter, const int& pos) {
    ...
}

Take a look at the argument types.看一下参数类型。 This function says "if someone tries adding together an honest-to-goodness Node<T> object and an int , here's what I'd like you to do."这个 function 说“如果有人试图将一个诚实的Node<T> object 和一个int相加,这就是我希望你做的。” The problem is that the code问题是代码

list.begin() + 2

doesn't try adding an honest-to-goodness Node<T> object and an integer.不会尝试添加诚实至善的Node<T> object 和 integer。 Instead, it adds a pointer to a Node<T> object and an integer.相反,它添加了一个指向Node<T> object 和 integer 的指针。 And since those types don't match your overloaded operator, it won't even try calling the overloaded operator.而且由于这些类型与您的重载运算符不匹配,它甚至不会尝试调用重载运算符。

Unfortunately, in C++ you can't overload an operator between two primitive types, so there's no way to write a version of operator+ that takes in a Node<T>* and an int , so the fix here isn't as simple as "just make your operator+ function take in a Node<T>* .不幸的是,在 C++ 中,您不能在两种基本类型之间重载运算符,因此无法编写一个接受Node<T>*intoperator+版本,因此这里的修复不像“只需让您的operator+ function 接受Node<T>*

Rather, I'd suggest making your iterator type an actual class or struct rather than a raw pointer.相反,我建议将您的iterator类型设为实际的classstruct ,而不是原始指针。 Your iterator will likely work by keeping track of a pointer to some Node<T> somewhere, but fundamentally the iterator isn't actually just that pointer itself.您的迭代器可能会通过跟踪指向某个Node<T>某处的指针来工作,但从根本上说,迭代器实际上不仅仅是该指针本身。 For example, you might try something like this:例如,您可以尝试这样的事情:

template <class T>
class List {
public:
    class iterator {
    public:
        // some other things, and
        iterator operator+ (int step) const;

    private:
        // some other things, and
        Node<T>* current;
    };

    // some other things, and
    iterator begin();
};

Now, you can overload operator+ on the List<T>::iterator type.现在,您可以在List<T>::iterator类型上重载operator+ That implementation of operator+ can then update the stored Node<T>* inside the iterator.然后, operator+的实现可以更新迭代器内存储的Node<T>*

Hope this helps!希望这可以帮助!

iterator for a linked list cannot be a pointer, it needs to be something like:链表的iterator不能是指针,它必须是这样的:

struct iterator
{   
    typedef int difference_type;
    typedef T* pointer;
    typedef T& reference;
    typedef iterator_category std::bidirectional_iterator_tag

    iterator();
    iterator& operator++();
    iterator& operator--();
    iterator operator++(int);
    iterator operator--(int);
    T& operator*();
    T* operator->();
    bool operator==(iterator rhs) const;
    bool operator!=(iterator rhs) const;
private:
    iterator(Node*);
    Node* node;
};

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

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