473,327 Members | 1,952 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,327 software developers and data experts.

Iterators within templates

Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
5: cout << *pos << " ";
6: cout << endl;
7: }
8:

The compiler says
Line 4 expected `;' before "pos"

I don't know what that's supposed to mean! Thanks.

Mar 26 '06 #1
2 1158
TB
Gaijinco skrev:
Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
for(typename vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
5: cout << *pos << " ";
6: cout << endl;
7: }
8:

The compiler says
Line 4 expected `;' before "pos"

I don't know what that's supposed to mean! Thanks.


--
TB @ SWEDEN
Mar 26 '06 #2
"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com
Why this doesn't work?

1: template <typename T>
2: void imprimir(vector<T> v)
3: {
4: for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)
5: cout << *pos << " ";
6: cout << endl;
7: }
8:

The compiler says
Line 4 expected `;' before "pos"

I don't know what that's supposed to mean! Thanks.


Please don't include line numbers with your code. Anyone who attempts to
compile it must first manually delete the numbers.

Replace

for(vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)

with

for(typename vector<T>::iterator pos=v.begin(); pos<v.end(); ++pos)

The compiler is interpreting vector<T>::iterator as a variable name, not as
a type name. Using typename tells it to interpret it as a type.

--
John Carson
Mar 26 '06 #3

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

Similar topics

14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
6
by: Kevin | last post by:
Hi All, Please could someone explain to me what an iterator is and how it may be used in a real world situation. I am new to C#, and I can't find a decent,plain english explanation for...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
19
by: John | last post by:
In STL's map implementation, the distance between two iterators it1, it2 takes O(K) to compute where K is the actual distance between the two iterators. In theory, a red black tree can do this...
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:...
2
by: Ole Nielsby | last post by:
Porting a project from VC8 to GCC, I stumbled on this: I have certain templates that use STL collections with a value type that is a pointer to a template parameter type. When I declare...
6
by: Rennie deGraaf | last post by:
Hello, I would like to write a function that reads a sequence of unsigned shorts from /any/ container, converts them to pairs of unsigned chars, and writes them to /any/ container. In other...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
11
by: Juha Nieminen | last post by:
I'm writing an STL-style data container, and this problem is puzzling me. The following code should demonstrate the problem: //---------------------------------------------------------- #include...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.