STL List iterator

Discussion in 'C++' started by maverick, Oct 24, 2006.

  1. maverick

    maverick Guest

    Consider the following class
    foo.h
    ---------------------------------------------------
    #include <list.h>
    using namespace std ;
    template < class CFooTemp> class CFoo
    {
    CFoo();
    ~CFoo() ;


    list<CFooTemp *> m_list ;

    list<CFooTemp *> iterator :: iter ;
    };


    list<CFooTemp *> iterator :: iter ;
    This line gives me a compiler error

    error: expected `;' before "iter"


    Any clues as to why this would happen ?

    Now i change to primitive type such as int it compiles fine
    maverick, Oct 24, 2006
    #1
    1. Advertising

  2. maverick

    Michael Guest

    > list<CFooTemp *> iterator :: iter ;
    > This line gives me a compiler error
    >
    > error: expected `;' before "iter"


    How about
    list<CFooTemp*>::iterator iter;

    Michael
    Michael, Oct 24, 2006
    #2
    1. Advertising

  3. maverick

    red floyd Guest

    maverick wrote:
    > Consider the following class
    > foo.h
    > ---------------------------------------------------
    > #include <list.h>

    No such header.

    #include <list>

    > using namespace std ;
    > template < class CFooTemp> class CFoo
    > {
    > CFoo();
    > ~CFoo() ;
    >
    >
    > list<CFooTemp *> m_list ;
    >
    > list<CFooTemp *> iterator :: iter ;

    typename list<CFooTemp *>::iterator iter;
    > };
    >
    >
    red floyd, Oct 24, 2006
    #3
  4. maverick

    Pete Becker Guest

    red floyd wrote:
    >
    > #include <list>
    >
    >> using namespace std ;
    >> template < class CFooTemp> class CFoo
    >> {
    >> CFoo();
    >> ~CFoo() ;
    >>
    >>
    >> list<CFooTemp *> m_list ;
    >>
    >> list<CFooTemp *> iterator :: iter ;

    > typename list<CFooTemp *>::iterator iter;


    typename isn't needed here: CFooTemp is not a dependent name. In fact,
    it used to be illegal, but that made writing code too hard (like the
    rules for semicolons in Pascal).

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Oct 24, 2006
    #4
  5. maverick

    red floyd Guest

    Pete Becker wrote:
    > red floyd wrote:
    >>
    >> #include <list>
    >>
    >>> using namespace std ;
    >>> template < class CFooTemp> class CFoo
    >>> {
    >>> CFoo();
    >>> ~CFoo() ;
    >>>
    >>>
    >>> list<CFooTemp *> m_list ;
    >>>
    >>> list<CFooTemp *> iterator :: iter ;

    >> typename list<CFooTemp *>::iterator iter;

    >
    > typename isn't needed here: CFooTemp is not a dependent name. In fact,
    > it used to be illegal, but that made writing code too hard (like the
    > rules for semicolons in Pascal).
    >


    OK, I'm thoroughly confused.

    If I had list<CFooTemp>::iterator, would I have needed a typename? If
    so, what's the difference between that and list<CFooTemp*>::iterator?
    red floyd, Oct 24, 2006
    #5
  6. maverick

    maverick Guest

    By adding the typename before the list my compilation error was
    eliminated


    red floyd wrote:
    > Pete Becker wrote:
    > > red floyd wrote:
    > >>
    > >> #include <list>
    > >>
    > >>> using namespace std ;
    > >>> template < class CFooTemp> class CFoo
    > >>> {
    > >>> CFoo();
    > >>> ~CFoo() ;
    > >>>
    > >>>
    > >>> list<CFooTemp *> m_list ;
    > >>>
    > >>> list<CFooTemp *> iterator :: iter ;
    > >> typename list<CFooTemp *>::iterator iter;

    > >
    > > typename isn't needed here: CFooTemp is not a dependent name. In fact,
    > > it used to be illegal, but that made writing code too hard (like the
    > > rules for semicolons in Pascal).
    > >

    >
    > OK, I'm thoroughly confused.
    >
    > If I had list<CFooTemp>::iterator, would I have needed a typename? If
    > so, what's the difference between that and list<CFooTemp*>::iterator?
    maverick, Oct 24, 2006
    #6
  7. maverick

    Guest

    Pete Becker wrote:
    > red floyd wrote:
    > >
    > > #include <list>
    > >
    > >> using namespace std ;
    > >> template < class CFooTemp> class CFoo
    > >> {
    > >> CFoo();
    > >> ~CFoo() ;
    > >>
    > >>
    > >> list<CFooTemp *> m_list ;
    > >>
    > >> list<CFooTemp *> iterator :: iter ;

    > > typename list<CFooTemp *>::iterator iter;

    >
    > typename isn't needed here: CFooTemp is not a dependent name.


    'iterator' is. It depends on CFooTemp. The compiler doesn't know
    iterator is a
    type when it's compiling CFoo. It only knows this when CFoo is
    instantiated
    because it can then pick the correct list specialization. That's too
    late.

    Regards,
    Michiel Salters
    , Oct 25, 2006
    #7
  8. maverick

    red floyd Guest

    wrote:
    > Pete Becker wrote:
    >> red floyd wrote:
    >>> #include <list>
    >>>
    >>>> using namespace std ;
    >>>> template < class CFooTemp> class CFoo
    >>>> {
    >>>> CFoo();
    >>>> ~CFoo() ;
    >>>>
    >>>>
    >>>> list<CFooTemp *> m_list ;
    >>>>
    >>>> list<CFooTemp *> iterator :: iter ;
    >>> typename list<CFooTemp *>::iterator iter;

    >> typename isn't needed here: CFooTemp is not a dependent name.

    >
    > 'iterator' is. It depends on CFooTemp. The compiler doesn't know
    > iterator is a
    > type when it's compiling CFoo. It only knows this when CFoo is
    > instantiated
    > because it can then pick the correct list specialization. That's too
    > late.
    >
    > Regards,
    > Michiel Salters
    >


    Thanks, Michiel. I thought I was going crazy.
    red floyd, Oct 25, 2006
    #8
    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. Replies:
    6
    Views:
    620
    Jim Langston
    Oct 30, 2005
  2. Jonathan
    Replies:
    6
    Views:
    402
    Jonathan
    Mar 14, 2006
  3. Replies:
    15
    Views:
    564
    Daniel T.
    Nov 29, 2006
  4. Olumide
    Replies:
    10
    Views:
    618
    Richard Herring
    Sep 17, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,019
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page