Problem with list<T>::iterator

Discussion in 'C++' started by Gaijinco, Apr 6, 2009.

  1. Gaijinco

    Gaijinco Guest

    This code doesn´t compile:

    template <typename T>
    ostream& operator<<(ostream& out, const list<T>& l) {

    list<T>::iterator it = l.begin();
    while(it != (l.end() - 1)) {
    out << *it << " ";
    ++it;
    }
    if(l.size() > 0) {
    out << *it;
    }

    return out;
    }

    My compiler says: "expected ';' before it"

    Why I'm doing wrong?

    Thanks.
    Gaijinco, Apr 6, 2009
    #1
    1. Advertising

  2. Gaijinco

    rishabh Guest

    On Apr 6, 8:03 am, Gaijinco <> wrote:
    > This code doesn´t compile:
    >
    > template <typename T>
    > ostream& operator<<(ostream& out, const list<T>& l) {
    >
    >    list<T>::iterator it = l.begin();
    >    while(it != (l.end() - 1)) {
    >       out << *it << " ";
    >       ++it;
    >    }
    >    if(l.size() > 0) {
    >       out << *it;
    >    }
    >
    >    return out;
    >
    > }
    >
    > My compiler says: "expected ';' before it"
    >
    > Why I'm doing wrong?
    >
    > Thanks.


    Please include #include <list>
    rishabh, Apr 6, 2009
    #2
    1. Advertising

  3. Gaijinco

    peter koch Guest

    On 6 Apr., 05:03, Gaijinco <> wrote:
    > This code doesn´t compile:
    >
    > template <typename T>
    > ostream& operator<<(ostream& out, const list<T>& l) {
    >
    >    list<T>::iterator it = l.begin();

    You need a typename here: typename list<T>:......

    Since this is templated code, you need to tell the compiler that
    iterator is a type.
    >    while(it != (l.end() - 1)) {

    And this will not compile - list iterators are not random.
    >       out << *it << " ";
    >       ++it;
    >    }
    >    if(l.size() > 0) {
    >       out << *it;
    >    }
    >
    >    return out;
    >
    > }


    /Peter
    peter koch, Apr 6, 2009
    #3
  4. peter koch schrieb:
    > On 6 Apr., 05:03, Gaijinco <> wrote:


    >> while(it != (l.end() - 1)) {

    > And this will not compile - list iterators are not random.


    And even if they were, the condition assumes that the list contains at
    least one element, and fails if the list is empty (probably leads to the
    condition never becoming true, at best).

    > /Peter


    /Peter
    Peter Remmers, Apr 6, 2009
    #4
  5. Gaijinco

    peter koch Guest

    On 6 Apr., 20:07, Peter Remmers
    <> wrote:
    > peter koch schrieb:
    >
    > > On 6 Apr., 05:03, Gaijinco <> wrote:
    > >>    while(it != (l.end() - 1)) {

    > > And this will not compile - list iterators are not random.

    >
    > And even if they were, the condition assumes that the list contains at
    > least one element, and fails if the list is empty (probably leads to the
    > condition never becoming true, at best).


    Yes - that is true as well - I focused on the syntax errors. The
    entire design could be argued. While I might have a function like the
    above for convenience, my central algorithm would be based on a pair
    of iterators.

    >
    > > /Peter

    >
    > /Peter


    /Peter
    peter koch, Apr 6, 2009
    #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. Hendrik Maryns
    Replies:
    18
    Views:
    1,393
  2. greg
    Replies:
    6
    Views:
    441
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    620
    Jim Langston
    Oct 30, 2005
  4. David Bilsby
    Replies:
    5
    Views:
    2,019
    David Bilsby
    Oct 9, 2007
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    107
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page