473,396 Members | 2,068 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,396 software developers and data experts.

Template & iterator Problem

Hi all, I have a question about passing iterators to a function. I want to create a function that does something with STL container iterators. The container of the iterator can vary, so I use a template container there. Here is the code:
Expand|Select|Wrap|Line Numbers
  1. template <class Container, class T>
  2. void do_sth(typename Container<T>::iterator &start, typename Container<T>::iterator &end, T &elem)
  3. {
  4.   //Some code here
  5. }
  6.  
I get compilation error (unrecognizable template declaration/definition) in MS VC++ 8.0.
Helps would be appreciated. Thanks...
Oct 1 '07 #1
2 1613
weaknessforcats
9,208 Expert Mod 8TB
You can pass iterators to a function but what you can't do is use the typename in a template as thougfh it were a class member.

This is OK:
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class Container
  3. {
  4.      T thedata;
  5. public:
  6.     class iterator
  7.     {
  8.         void AMethod();
  9.  
  10.     };
  11. };
  12. template <class T>
  13. void Container<T>::iterator::AMethod()
  14. {
  15.  
  16. }
  17.  
Here the syntax Container<T>::iterator is used to identify a member function of Container<T>. Unless you are writing a member function you can't use this syntax.

What you should code is:
Expand|Select|Wrap|Line Numbers
  1. template < class T, class U>
  2. void do_sth(U& start, U& end, T elem)
  3. {
  4.   //Some code here
  5. }
  6.  
  7. //and in main():
  8.  
  9. int main()
  10. {
  11.   Container<int> c;
  12.   Container<int>::iterator itr;
  13.   do_sth(itr, itr, 5);
  14. }
  15.  
Oct 1 '07 #2
Thanks a lot, that solved my problem.
Oct 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
3
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
1
by: darkstorm | last post by:
Please check this program...When I compiles it in VC.net, it gives the following error: =============== Common\Lib\PList.h(115): error C2440: '=' : cannot convert from 'ListNode *' to...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
2
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public:...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
8
by: flamexx7 | last post by:
Can anybody tell me what is wrong with declaration of pointer p ? template<class Tclass Stack { struct Link { T* data; Link* next; Link(T* dat, Link* nxt) : data(dat), next(nxt) {} }* head;...
8
by: Fab | last post by:
All, I need your help understanding why the following code does *NOT* compile with G++ (tested with gcc 3.x and 4.1.x): ---------------------------------------------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.