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

Troubles when using STL iterator

Hi newsgroup,

the following code cannot be compiled and used on my system.

------------- CUT HERE -------------
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <iostream>
#include <vector>

class myclass
{
template<typename T>
void f(const std::vector<T> &p)
{
std::vector<T>::const_iterator iter = p.begin();
....
....
....
}
};

#endif /*MYCLASS_H_*/

------------- CUT HERE -------------

When i try to compile it on my system, the compiler reports the following
error message: expected ';' before 'iter'.

On the other hand, when i try to use the class method f() as a normal
function within (lets say) the main.cpp file, the code can be compiled and
executed. But why?

Can someone help me?

Thank you in advance!
Mike
May 1 '06 #1
5 5466
Michael wrote:
Hi newsgroup,

the following code cannot be compiled and used on my system.

------------- CUT HERE -------------
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <iostream>
#include <vector>

class myclass
{
template<typename T>
void f(const std::vector<T> &p)
{
std::vector<T>::const_iterator iter = p.begin();
....
....
....
}
};

#endif /*MYCLASS_H_*/

------------- CUT HERE -------------

When i try to compile it on my system, the compiler reports the following
error message: expected ';' before 'iter'.

On the other hand, when i try to use the class method f() as a normal
function within (lets say) the main.cpp file, the code can be compiled and
executed. But why?

Your compiler appears to be broken, what is it?

--
Ian Collins.
May 1 '06 #2
* Ian Collins:
* Michael:
template<typename T>
void f(const std::vector<T> &p)
{
std::vector<T>::const_iterator iter = p.begin();
....
....
....
}

When i try to compile it on my system, the compiler reports the following
error message: expected ';' before 'iter'.

On the other hand, when i try to use the class method f() as a normal
function within (lets say) the main.cpp file, the code can be compiled and
executed. But why?

Your compiler appears to be broken, what is it?


It's probably not a broken compiler.

There should be a 'typename' in there:

typename std::vector<T>::const_iterator iter = p.begin();

Otherwise, the compiler has no guarantee that const_iterator is a type
for all possible types T.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 1 '06 #3
On Mon, 01 May 2006 11:37:18 +0200, "Alf P. Steinbach"
<al***@start.no> wrote:
* Ian Collins:
Your compiler appears to be broken, what is it?
It's probably not a broken compiler.


It's probably a broken language :-)
There should be a 'typename' in there:

typename std::vector<T>::const_iterator iter = p.begin();

Otherwise, the compiler has no guarantee that const_iterator is a type
for all possible types T.


See also:
http://womble.decadentplace.org.uk/c...disambiguation

Best regards,
Roland Pibinger
May 1 '06 #4
Alf P. Steinbach wrote:
* Ian Collins:
* Michael:
template<typename T>
void f(const std::vector<T> &p)
{
std::vector<T>::const_iterator iter = p.begin();
....
....
....
}

When i try to compile it on my system, the compiler reports the
following
error message: expected ';' before 'iter'.
On the other hand, when i try to use the class method f() as a normal
function within (lets say) the main.cpp file, the code can be
compiled and
executed. But why?

Your compiler appears to be broken, what is it?

It's probably not a broken compiler.

There should be a 'typename' in there:

typename std::vector<T>::const_iterator iter = p.begin();

Otherwise, the compiler has no guarantee that const_iterator is a type
for all possible types T.

True I missed that (as does Sun CC), gcc even tells you where to add the
typename.

Still, the OP's compiler should have given a better error.

--
Ian Collins.
May 1 '06 #5
On Mon, 01 May 2006 11:37:18 +0200, Alf P. Steinbach wrote:
* Ian Collins:
* Michael:
template<typename T>
void f(const std::vector<T> &p)
{
std::vector<T>::const_iterator iter = p.begin();
....
....
....
}

When i try to compile it on my system, the compiler reports the following
error message: expected ';' before 'iter'.

On the other hand, when i try to use the class method f() as a normal
function within (lets say) the main.cpp file, the code can be compiled and
executed. But why?

Your compiler appears to be broken, what is it?


It's probably not a broken compiler.

There should be a 'typename' in there:

typename std::vector<T>::const_iterator iter = p.begin();

Otherwise, the compiler has no guarantee that const_iterator is a type
for all possible types T.


Hi

That was the reason: typename was missing ...
Thank you for your help.

Mike

May 1 '06 #6

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

Similar topics

16
by: Dave Smithz | last post by:
Hi, In summary: I want to a form to submit information via a HTTP POST, however, when using Internet Explorer I want to be able to use the back button and all the information retained....
11
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it...
1
by: SammyBar | last post by:
Hi all, I'm having troubles with a Symbol 9000 device (Compact Framework v 1.1) when activating the barcode scanner from a window. The problem is related to the Activated event of the form which...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
7
by: Donos | last post by:
Hello I have a Queue which is declared as, std::queue<unsigned charm_Queue; I push data into this queue. Now i want to take the data out of this queue using Iterator.
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
13
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?

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.