Pregunta

I am working on a circular, doubly-linked list for a class assignment, and I am having some trouble with it. When I try placing my iterator into either my own prefix_sum method or the STL version (partial_sum), it gives an error on the lines *result = val; and *++result = val; (and the equivalent lines of partial_sum), stating that an lvalue is required as left operand of assignment. The STL version of iterator works fine in my prefix_sum method, so I know it's a problem somewhere in my iterator, but I can't place it.

Any help would be greatly appreciated.

My header file:

#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

My (abridged) implementation file:

/**
 * @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;
    }
}
¿Fue útil?

Solución

The reason this does not work is that you did not overload the operator correctly: operator * should return a reference to T, like this:

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

Note that since you are defining your own iterator, you should also provide an overload for the infix operator ->. You should also provide a pair of const overloads, so in the end you should have these four operators:

T& operator*();
const T& operator*() const;
T* operator->();
const T* operator->() const;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top