STL::Accessing the iterator for a "passed" container type

Discussion in 'C++' started by Generic Usenet Account, Oct 5, 2007.

  1. Hi,

    I am passing a container type (e.g. list<int>) to a function template
    but I am getting a compiler error when I try to declare an iterator
    for that type in the function. However, I am able to invoke begin()
    and end() without any problem. Is there a way to declare an iterator
    for the container type? Sample code follows.

    Thanks,
    Ramesh


    //////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////
    #include <iostream>
    #include <list>

    using namespace std;

    //////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////
    template<typename T>
    void
    access_the_iterator(T& collection)
    {
    while(collection.begin() != collection.end())
    {
    cout << "In the loop\n";
    }

    //T::iterator iter;
    // ** Compiler error upon uncommenting **
    }


    //////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////
    main()
    {
    list<int> coll;
    access_the_iterator(coll);
    return 0;
    }
     
    Generic Usenet Account, Oct 5, 2007
    #1
    1. Advertising

  2. On Fri, 05 Oct 2007 15:40:29 -0700, Generic Usenet Account wrote:
    > Hi,
    >
    > I am passing a container type (e.g. list<int>) to a function template
    > but I am getting a compiler error when I try to declare an iterator for
    > that type in the function. However, I am able to invoke begin() and
    > end() without any problem. Is there a way to declare an iterator for
    > the container type? Sample code follows.


    [snip]

    > template<typename T>
    > void
    > access_the_iterator(T& collection)
    > {
    > while(collection.begin() != collection.end()) {
    > cout << "In the loop\n";
    > }
    >
    > //T::iterator iter;


    Try

    typename T::iterator iter;

    > // ** Compiler error upon uncommenting **
    > }


    --
    Markus Schoder
     
    Markus Schoder, Oct 6, 2007
    #2
    1. Advertising

  3. Generic Usenet Account

    Andre Kostur Guest

    Generic Usenet Account <> wrote in
    news::

    > Hi,
    >
    > I am passing a container type (e.g. list<int>) to a function template
    > but I am getting a compiler error when I try to declare an iterator
    > for that type in the function. However, I am able to invoke begin()
    > and end() without any problem. Is there a way to declare an iterator
    > for the container type? Sample code follows.


    That's what is referred to as a dependant type name. You need the
    "typename" keyword:

    typename T::iterator iter;


    C++ needs the hint that T::iterator is a type and not a function or
    variable name. (Remember, it doesn't know what T is when it parses that
    line)
     
    Andre Kostur, Oct 6, 2007
    #3
    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,544
    Martijn Lievaart
    Feb 4, 2004
  2. forester
    Replies:
    16
    Views:
    738
    Howard Hinnant
    Aug 25, 2005
  3. Replies:
    4
    Views:
    831
    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:
    589
    Christoph Heindl
    Apr 4, 2006
  5. Urs Thuermann
    Replies:
    6
    Views:
    515
    Urs Thuermann
    Nov 4, 2011
Loading...

Share This Page