1

I am new to C++ still, I feel this must be quite obvious mistake that I am missing.

#include <iostream>
#include "List.h"

int main()
{
    List<int> mylist;
    List<int>::iterator it1, it2;

this line

List<int>::iterator it1, it2;

gives the message "List has no member iterator"

here is the List Class

template <typename Object>
class List
{
private:
    // The basic doubly linked List node.
    // Nested inside of List, can be public
    // because the Node is itself private
    class Node
    {
        Object  _data;
        Node   *_prev;
        Node   *_next;

        Node(const Object & d = Object(), Node * p = NULL, Node * n = NULL)
            : _data(d), _prev(p), _next(n) { }
    };

public:




public:
    List()
    {
        init();
    }

    ~List()
    {
        clear();
        delete _head;
        delete _tail;
    }

    List(const List & rhs)
    {
        init();
        *this = rhs;
    }

    const List & operator= (const List & rhs)
    {
        if (this == &rhs)
            return *this;
        clear();
        for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr)
            push_back(*itr);
        return *this;
    }

    // Return iterator representing beginning of List.
    // Mutator version is first, then accessor version.
    iterator begin()
    {
        return iterator(_head->_next);
    }

    const_iterator begin() const
    {
        return const_iterator(_head->_next);
    }

    // Return iterator representing endmarker of List.
    // Mutator version is first, then accessor version.
    iterator end()
    {
        return iterator(_tail);
    }

    const_iterator end() const
    {
        return const_iterator(_tail);
    }

    // Return number of elements currently in the List.
    int size() const
    {
        return _size;
    }

    // Return true if the List is empty, false otherwise.
    bool empty() const
    {
        return size() == 0;
    }

    void clear()
    {
        while (!empty())
            pop_front();
    }

    // front, back, push_front, push_back, pop_front, and pop_back
    // are the basic double-ended queue operations.
    Object & front()
    {
        return *begin();
    }

    const Object & front() const
    {
        return *begin();
    }

    Object & back()
    {
        return *--end();
    }

    const Object & back() const
    {
        return *--end();
    }

    void push_front(const Object & x)
    {
        insert(begin(), x);
    }

    void push_back(const Object & x)
    {
        insert(end(), x);
    }

    void pop_front()
    {
        erase(begin());
    }

    void pop_back()
    {
        erase(--end());
    }

    // Insert x before itr.
    iterator insert(iterator itr, const Object & x)
    {
        Node *p = itr.current;
        _size++;
        return iterator(p->_prev = p->_prev->_next = new Node(x, p->_prev, p));
    }

    // Erase item at itr.
    iterator erase(iterator itr)
    {
        Node *p = itr.current;
        iterator retVal(p->_next);
        p->_prev->_next = p->_next;
        p->_next->_prev = p->_prev;
        delete p;
        _size--;

        return retVal;
    }

    iterator erase(iterator start, iterator end)
    {
        for (iterator itr = start; itr != end; )
            itr = erase(itr);

        return end;
    }

private:
    int   _size;
    Node *_head;
    Node *_tail;

    void init()
    {
        _size = 0;
        _head = new Node;
        _tail = new Node;
        _head->_next = _tail;
        _tail->_prev = _head;
    }
};

I am assuming that this error is probably for not having an Iterator class?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Random Joe
  • 19
  • 4
  • 1
    Where's your declaration/definition for your nested `List::iterator` class?? – πάντα ῥεῖ Feb 02 '17 at 17:59
  • @Dan `std::list` probably doesn't fulfill the educational purpose of that exercise. – πάντα ῥεῖ Feb 02 '17 at 18:01
  • All the code is posted, so if you are asking where the declaration of the List::iterator class is, it merely doesn't exist and that must be the issue. I figured I didn't need one since I have all the iterator functions declared/defined – Random Joe Feb 02 '17 at 18:06
  • @Dan I'm with you. [Here I gave similar arguments](http://dev-jungle.blogspot.de/2015/02/software-development-in-wild-i-have.html) what's seemingly wrong with a major number of _C++ classes and courses_. – πάντα ῥεῖ Feb 02 '17 at 18:06
  • @RandomJoe _"it merely doesn't exist and that must be the issue."_ Exactly. The candidate earns 5 points. – πάντα ῥεῖ Feb 02 '17 at 18:07
  • Possible duplicate of [How to implement an STL-style iterator and avoid common pitfalls?](http://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls) – πάντα ῥεῖ Feb 02 '17 at 18:10
  • You don't have a member "iterator", just some methods that have the returntype iterator. – Alex Feb 02 '17 at 18:16
  • IMO, assignments like this are a waste of time. In the real world, how many students even complete an assignment like this, *and complete it correctly*? In the end, it takes an experienced C++ programmer to practically give the answer to the student, thus the student learns nothing (except getting a glimpse of how the code should be written). – PaulMcKenzie Feb 02 '17 at 18:35
  • Even the code posted does the topsy-turvy method of calling the assignment operator from the copy constructor, when ideally, the copy constructor should be called from the assignment operator (as in the copy / swap idiom). – PaulMcKenzie Feb 02 '17 at 19:06

1 Answers1

2

I am assuming that this error is probably for not having an Iterator class?

Yes, your code supposes that List<T> also provides a nested List<T>::iterator class.

That's what's missing in your code.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190