Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL::Accessing the iterator for a "passed" container type (http://www.velocityreviews.com/forums/t542330-stl-accessing-the-iterator-for-a-passed-container-type.html)

Generic Usenet Account 10-05-2007 10:40 PM

STL::Accessing the iterator for a "passed" container type
 
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;
}


Markus Schoder 10-05-2007 11:08 PM

Re: STL::Accessing the iterator for a "passed" container type
 
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

Andre Kostur 10-05-2007 11:09 PM

Re: STL::Accessing the iterator for a "passed" container type
 
Generic Usenet Account <usenet@sta.samsung.com> wrote in
news:1191624029.029922.93220@57g2000hsv.googlegrou ps.com:

> 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)


All times are GMT. The time now is 12:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.