CAnnot find the compilation error in this circular list

Discussion in 'C++' started by Vince, Nov 29, 2004.

  1. Vince

    Vince Guest

    Hi,

    I am not used to template and I cannot find what's wrong with this code :

    Compiler(MSVS 7) complains at line :
    class iterator : public std::iterator<...



    Here is the source code

    #include <iostream>
    #include <list>
    #include <string>
    using namespace std;

    template<class T>
    class Ring {
    list<T> lst;
    public:
    // Declaration necessary so the following
    // 'friend' statement sees this 'iterator'
    // instead of std::iterator:
    class iterator;
    friend class iterator;
    class iterator : public std::iterator<
    std::bidirectional_iterator_tag,T,ptrdiff_t>{
    list<T>::iterator it;
    list<T>* r;
    public:
    // "typename" necessary to resolve nesting:
    iterator(list<T>& lst,
    const typename list<T>::iterator& i)
    : r(&lst), it(i) {}
    bool operator==(const iterator& x) const {
    return it == x.it;
    }
    bool operator!=(const iterator& x) const {
    return !(*this == x);
    }
    list<T>::reference operator*() const {
    return *it;
    }
    iterator& operator++() {
    ++it;
    if(it == r->end())
    it = r->begin();
    return *this;
    }
    iterator operator++(int) {
    iterator tmp = *this;
    ++*this;
    return tmp;
    }
    iterator& operator--() {
    if(it == r->begin())
    it = r->end();
    --it;
    return *this;
    }
    iterator operator--(int) {
    iterator tmp = *this;
    --*this;
    return tmp;
    }
    iterator insert(const T& x){
    return iterator(*r, r->insert(it, x));
    }
    iterator erase() {
    return iterator(*r, r->erase(it));
    }
    };
    void push_back(const T& x) {
    lst.push_back(x);
    }
    iterator begin() {
    return iterator(lst, lst.begin());
    }
    int size() { return lst.size(); }
    };
     
    Vince, Nov 29, 2004
    #1
    1. Advertising

  2. Vince wrote:
    > I am not used to template and I cannot find what's wrong with this code :
    >
    > Compiler(MSVS 7) complains at line :
    > class iterator : public std::iterator<...


    Have you tried adding #include <iterator> ? Just a guess...

    >
    > [...]


    V
     
    Victor Bazarov, Nov 29, 2004
    #2
    1. Advertising

  3. Vince

    Vince Guest

    No it doesn't work.

    "Victor Bazarov" <> a écrit dans le message de news:
    0yMqd.11492$09.us.to.verio.net...
    > Vince wrote:
    >> I am not used to template and I cannot find what's wrong with this code :
    >>
    >> Compiler(MSVS 7) complains at line :
    >> class iterator : public std::iterator<...

    >
    > Have you tried adding #include <iterator> ? Just a guess...
    >
    >>
    >> [...]

    >
    > V
     
    Vince, Nov 29, 2004
    #3
  4. Vince wrote:
    > No it doesn't work.
    >
    > "Victor Bazarov" <> a écrit dans le message de news:
    > 0yMqd.11492$09.us.to.verio.net...
    >
    >>Vince wrote:
    >>
    >>>I am not used to template and I cannot find what's wrong with this code :
    >>>
    >>> Compiler(MSVS 7) complains at line :
    >>>class iterator : public std::iterator<...

    >>
    >>Have you tried adding #include <iterator> ? Just a guess...
    >>
    >>
    >>>[...]


    Then you should probably read the FAQ 5.8...
     
    Victor Bazarov, Nov 29, 2004
    #4
  5. Vince

    Old Wolf Guest

    "Vince" <> wrote:

    > I am not used to template and I cannot find what's wrong with this code :
    >
    > Compiler(MSVS 7) complains at line :
    > class iterator : public std::iterator<...
    >
    > Here is the source code
    >
    > using namespace std;


    How about not calling your class 'iterator' when there is
    already a class called 'iterator' in scope?

    Alternatively, drop the "using namespace std;" line. It's
    generally a bad idea to have that line in a header file anyway
    because it forces everyone who uses your header to also have
    std included, and they may not want to.
     
    Old Wolf, Nov 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    761
    Jonathan Mcdougall
    Jan 3, 2005
  2. Sven
    Replies:
    1
    Views:
    166
    Roy Smith
    Mar 7, 2013
  3. Sven
    Replies:
    3
    Views:
    173
    Steven D'Aprano
    Mar 8, 2013
  4. Chris Angelico
    Replies:
    0
    Views:
    134
    Chris Angelico
    Mar 7, 2013
  5. Chris Rebert
    Replies:
    0
    Views:
    133
    Chris Rebert
    Mar 7, 2013
Loading...

Share This Page