473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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<intcoll;
access_the_iterator(coll);
return 0;
}

Oct 5 '07 #1
3 1282
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
Oct 5 '07 #2
Generic Usenet Account <us****@sta.samsung.comwrote in
news:11*********************@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)
Oct 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
10
by: Piotr | last post by:
In Effective STL item 9 "Choose carefully among erasing options", it has this example: bool badValue(int x); // returns whether x is 'bad' c.erase ( remove_if(c.begin(), c.end(), badValue),...
8
by: pvonnied | last post by:
Hi, Once more a question (sorry for the different e-mail addresses , here @ work we have to use Google to post to newsgroups): If I use an iterator, say from std::map, I initialize it like so:...
9
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
10
by: Boltar | last post by:
Hello I need to iterate through an STL list container and delete certain entries in it. I realise if use erase() with the iterator it will invalidate it but what if I store it beforehand? Ie: is...
27
by: Jason Doucette | last post by:
I'm getting an assertion fire from a list iterator being checked against NULL. This did not occur in VC++ 2003 (v7.1). Are there changes that have been made to the STL between these versions that...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.