List Iterator,“左值作为赋值的左值”

时间:2013-11-29 23:04:54

标签: c++ list templates iterator listiterator

我正在为一个课程作业制作一个循环的,双重链接的列表,我遇到了一些麻烦。当我尝试将迭代器放入我自己的prefix_sum方法或STL版本(partial_sum)时,它会在行*result = val;*++result = val;上给出错误(等效partial_sum)行,表示lvalue is required as left operand of assignment。 迭代器的STL版本在我的prefix_sum方法中工作正常,所以我知道它在我的迭代器中的某个地方是个问题,但是我不能放置它。

非常感谢任何帮助。

我的标题文件:

#ifndef LIST_H
#define LIST_H

#include <string>
#include <iostream>

template<class T>
struct Node
{
    Node* prev;
    Node* next;
    T val;

    Node():prev(this), next(this) {}

    Node(T aValue) :prev (this), next(this), val(aValue) {}
};

template<class T>
class s_list
{
  public:
    struct iterator
    {
        typedef std::bidirectional_iterator_tag iterator_category;
        typedef std::ptrdiff_t difference_type;
        typedef T value_type;
        typedef T * pointer;
        typedef T &reference;

      public:
        // Constructors:
        iterator() :cur(new Node<T>()) {}

        // Operatoral Overloads
        T operator *() {return cur->val;}
        bool operator == (const iterator an_iter) {return (cur == an_iter.cur);}
        bool operator != (const iterator an_iter) {return (cur != an_iter.cur);}
        iterator& operator ++ () {cur = cur->next; return *this;}
        iterator& operator -- () {cur = cur->prev; return *this;}
        iterator operator ++ (int)
        {
            iterator temp = *this;
            cur = cur->next;
            return temp;
        }
        iterator operator -- (int)
        {
            iterator temp = *this;
            cur = cur->prev;
            return temp;
        }

        friend class s_list;

      private:
        iterator(Node<T>* to_iter):cur(to_iter) {}
        Node<T>* cur;
    };

    // Constructors:
    s_list() : head(new Node<T>()){}
    s_list(const s_list<T>& aList) : head(new Node<T>()) {...}

    // Destructor:
    virtual ~s_list() {clear(); delete head;}

    // Iterator Methods:
    iterator begin() {return (iterator(head->next));}
    iterator end() {return (iterator(head));}

    void push_back(T aVal) {...}

    void push_front(T aVal) {...}

    void insert(iterator pos, T aVal) {...}

    int size() {...}

    bool empty() {return (head->next == head->prev);}
    inline void erase(iterator to_del);
    inline void erase(iterator from, iterator to);
    void clear(){erase(head->next, head);}
    //inline void prefix_sum(iterator start_sum, iterator end_sum, iterator write_to);

    // Operational Overloads:
    inline void operator = (const s_list<T>& aList);

  private:
    Node<T>* head;
};


template <class iterator_in, class iterator_out>
iterator_out prefix_sum (iterator_in first, iterator_in last, iterator_out result)
{
    std::cerr << "\n*result = " << *result << '\n';
    if (first != last) {
        typename std::iterator_traits<iterator_in>::value_type val = *first;

        *result = val;

        while (++first != last) {
            val = val + *first;
            *++result = val;
        }

        ++result;
    }

    return result;
}


#endif // LIST_H

我的(删节)实施文件:

/**
 * @date 24 November, 2013
 * @brief Linked List 6
 * @file HW7.cpp
 *
 * @note
 * This work is licensed under a Creative Commons Attribution-NonCommercial 3.0
 * Unported License.
 *
 * Permission is granted to copy, distribute, transmit, and/or adapt this software
 * for any and all noncommercial purposes.
 *
 * For details, see:
 * https://creativecommons.org/licenses/by-nc/3.0/
 */

#include "s_list.h"
#include <iostream>
#include <sstream>
#include <list>
#include <numeric>
    for(int i = 1; i < 10; i++)
    {
        stlList.push_back(i);
        myList.push_back(i);
    }

    std::cout << "\nOriginal myList:\n";

    typename s_list<int>::iterator myIter = myList.begin();

    while (myIter != myList.end()){
        std::cout << *myIter << "  ";
        ++myIter;
    }

    std::cout << "\nOriginal stlList:\n";

    std::_List_iterator<int> stlIter = stlList.begin();

    while (stlIter != stlList.end()){
        std::cout << *stlIter << "  ";
        ++stlIter;
    }
    std::partial_sum(myList.begin(), myList.end(), (myList.begin()));
    prefix_sum(myList.begin(), myList.end(), myList.begin());
    prefix_sum(stlList.begin(), stlList.end(), stlList.begin());

    std::cout << "\nResult after running myList with STL partial_sum() algorithm\n";

    myIter = myList.begin();
    while(myIter != myList.end()){
        std::cout << *myIter << "  ";
        ++myIter;
    }


    std::cout << "\nResult after running STL list with my prefix_sum() algorithm\n";

    stlIter = stlList.begin();
    while (stlIter != stlList.end()){
        std::cout << *stlIter<< "  ";
        ++stlIter;
    }
}

1 个答案:

答案 0 :(得分:2)

这不起作用的原因是你没有正确地重载操作符:operator *应该返回对T的引用,如下所示:

T& operator *() { return cur->val; }
 ^
 |
 +-- Return type needs to be a reference type.

请注意,由于您要定义自己的迭代器,因此还应为中缀运算符->提供重载。你还应该提供一对const重载,所以最后你应该有这四个运算符:

T& operator*();
const T& operator*() const;
T* operator->();
const T* operator->() const;