Custom iterator for STL-style container

Discussion in 'C++' started by gallows, Mar 5, 2007.

  1. gallows

    gallows Guest

    The container is:

    template <typename T>
    class Container {
    public:
    // container methods..

    // iterator:
    class const_iterator {
    public:
    const_iterator(T* i)
    : pntr(i) { }

    const_iterator& operator=
    (const_iterator);
    const_iterator& operator++();
    const_iterator operator++(const_iterator);
    const_iterator& operator--();
    const_iterator operator--(const_iterator);
    bool operator==(const_iterator);
    bool operator!=(const_iterator);

    private:
    T* pntr;
    };

    const_iterator begin() const;
    const_iterator end() const;

    private:
    std::vector<T> v;
    };

    // Well... implementation for Container<T>::begin() :
    template <typename T>
    Container<T>::const_iterator Container<T>::begin() const
    {
    const_iterator i(&v[0]);
    return i;
    }

    g++ says: "error: expected constructor, destructor, or type conversion
    before 'Container'"
    Where is it wrong? Which is the right way to do that?

    Thanks in advance,

    s.
    gallows, Mar 5, 2007
    #1
    1. Advertising

  2. "gallows" ,comp.lang.c++:

    > template <typename T>
    > Container<T>::const_iterator Container<T>::begin() const


    Container<T>::const_iterator is a qualified (by Container<T>) and
    dependent (on T) name; therefore, you have to prefix it by the keyword
    "typename" to disambiguate the fact that it is the name of a type
    (Container<T>::const_iterator could very well be the name of a data
    member for some T).
    Pierre Senellart, Mar 5, 2007
    #2
    1. Advertising

  3. gallows

    mlimber Guest

    On Mar 5, 10:49 am, "gallows" <> wrote:
    > The container is:
    >
    > template <typename T>
    > class Container {
    > public:
    > // container methods..
    >
    > // iterator:
    > class const_iterator {
    > public:
    > const_iterator(T* i)
    > : pntr(i) { }
    >
    > const_iterator& operator=
    > (const_iterator);
    > const_iterator& operator++();
    > const_iterator operator++(const_iterator);


    This should be:

    const_iterator operator++(int);

    > const_iterator& operator--();
    > const_iterator operator--(const_iterator);


    This should be:

    const_iterator operator--(int);

    > bool operator==(const_iterator);
    > bool operator!=(const_iterator);
    >
    > private:
    > T* pntr;
    > };
    >
    > const_iterator begin() const;
    > const_iterator end() const;
    >
    > private:
    > std::vector<T> v;
    >
    > };
    >
    > // Well... implementation for Container<T>::begin() :
    > template <typename T>
    > Container<T>::const_iterator Container<T>::begin() const


    Add "typename" (see http://womble.decadentplace.org.uk/c /template-faq.html#type-syntax-error):

    typename Container<T>::const_iterator Container<T>::begin() const

    > {
    > const_iterator i(&v[0]);
    > return i;
    >
    > }
    >
    > g++ says: "error: expected constructor, destructor, or type conversion
    > before 'Container'"
    > Where is it wrong? Which is the right way to do that?


    Cheers! --M
    mlimber, Mar 5, 2007
    #3
  4. gallows

    gallows Guest

    Clear, I Understood. Thanks a lot!
    gallows, Mar 5, 2007
    #4
    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. Vivi Orunitia
    Replies:
    11
    Views:
    4,473
    Martijn Lievaart
    Feb 4, 2004
  2. forester
    Replies:
    16
    Views:
    716
    Howard Hinnant
    Aug 25, 2005
  3. Replies:
    4
    Views:
    797
    Daniel T.
    Feb 16, 2006
  4. Christoph  Heindl

    STL inherit from container<T>::iterator

    Christoph Heindl, Apr 3, 2006, in forum: C++
    Replies:
    2
    Views:
    570
    Christoph Heindl
    Apr 4, 2006
  5. Replies:
    2
    Views:
    395
Loading...

Share This Page