Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Generic Usenet Account
Guest
 
Posts: n/a
#1: Oct 5 '07
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<intcoll;
access_the_iterator(coll);
return 0;
}


Markus Schoder
Guest
 
Posts: n/a
#2: Oct 6 '07

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


On Fri, 05 Oct 2007 15:40:29 -0700, Generic Usenet Account wrote:
Quote:
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]
Quote:
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;
Quote:
// ** Compiler error upon uncommenting **
}
--
Markus Schoder
Andre Kostur
Guest
 
Posts: n/a
#3: Oct 6 '07

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


Generic Usenet Account <usenet@sta.samsung.comwrote in
news:1191624029.029922.93220@57g2000hsv.googlegrou ps.com:
Quote:
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)
Guest
 
Posts: n/a
#4: Oct 6 '07

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



http://www.arabzwaj.com/welcome/viewtopic.php?t=2457


http://www.arabzwaj.com/welcome/viewtopic.php?t=2455
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
http://www.arabzwaj.com/welcome/viewtopic.php?t=2456



11
http://www.arabzwaj.com/welcome/viewtopic.php?t=2458






Closed Thread