473,327 Members | 2,007 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.

template & STL

Hi, I'm trying to compile this template:

1 #include <set>
2
3 using std::set;
4
5 template <class TYPE>
6 void function(const set<TYPE&data) {
7 for (set<TYPE>::const_iterator iter = data.begin(); iter !=
data.end(); iter++) {
8 }
9 }

g++ 4.2.3 prints:
g++ temp.cpp -o temp
temp.cpp: In function ‘void function(const std::set<TYPE,
std::less<_Key>, std::allocator<_CharT&)’:
temp.cpp:7: error: expected `;' before ‘iter’
temp.cpp:7: error: ‘iter’ was not declared in this scope
make: *** [temp] Error 1

What is wrong ??

regards
Sep 19 '08 #1
2 1296
joe
On Sep 19, 1:44*pm, dawid <Dawid.Sierad...@gmail.comwrote:
Hi, I'm trying to compile this template:

* 1 #include <set>
* 2
* 3 using std::set;
* 4
* 5 template <class TYPE>
* 6 void function(const set<TYPE&data) {
* 7 * for (set<TYPE>::const_iterator iter = data.begin(); iter !=
data.end(); iter++) {
* 8 * }
* 9 }

g++ 4.2.3 prints:
g++ * * temp.cpp * -o temp
temp.cpp: In function ‘void function(const std::set<TYPE,
std::less<_Key>, std::allocator<_CharT&)’:
temp.cpp:7: error: expected `;' before ‘iter’
temp.cpp:7: error: ‘iter’ was not declared in this scope
make: *** [temp] Error 1

What is wrong ??

regards
You have to give the compiler a hint here.
Try adding the "typename" keyword :
for (typename set<TYPE>::const_iterator iter ...

It's just one of those things you have to train yourself to think of
when you see an error message like that in a template.
It's sort of like remembering when e comes before i.
Joe Cook
Sep 19 '08 #2
On 19 Sep., 19:44, dawid <Dawid.Sierad...@gmail.comwrote:
Hi, I'm trying to compile this template:

* 1 #include <set>
* 2
* 3 using std::set;
* 4
* 5 template <class TYPE>
* 6 void function(const set<TYPE&data) {
* 7 * for (set<TYPE>::const_iterator iter = data.begin(); iter !=
data.end(); iter++) {
* 8 * }
* 9 }

g++ 4.2.3 prints:
g++ * * temp.cpp * -o temp
temp.cpp: In function ‘void function(const std::set<TYPE,
std::less<_Key>, std::allocator<_CharT&)’:
temp.cpp:7: error: expected `;' before ‘iter’
temp.cpp:7: error: ‘iter’ was not declared in this scope
make: *** [temp] Error 1

What is wrong ??
You forgot a typename: line 7 should read:
for (typename set<TYPE>::const_iterator iter = data.begin(); iter !
=

The reason is template specialisation: a template class could be
specialised, meaning that set<TYPEmight not have a const_iterator.
You and I know that this is silly for std::set, but the compiler does
not.

/Peter
Sep 19 '08 #3

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

Similar topics

1
by: bjam | last post by:
Hi, if I want to include a set of call template methods into my xsl file can I do that? Meaning I want to have the template method below of data_output in another file that I can use in several...
2
by: Frank Schmitt | last post by:
Hi! Suppose I have a class template Foo with a member function template bar - so far so good: template <typename T> struct Foo { template <typename U> void bar(const U& u); };
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...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
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:...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
8
by: mast2as | last post by:
Hi guys, I think from what I found on the net that the code is correct and there's no problem when I compile the files. The problems occurs when I link them. I have a friend function which outputs...
1
by: Aggelos | last post by:
Dear developers, I am trying to find an answer to that rpoblem of mine for a very long time now... and still I had no luck. I have my tinyMCE WYSIWYG and I want to let people create an e-mail...
5
by: akilageethal | last post by:
I want to get a png template processed . I am trying to call a CGI script to process the template . for the image url iam giving the CGI script path. bugsummeryperiod.cgi?&amp;type=4&amp; here iam...
1
by: matz2k | last post by:
I've got a big problem with the CSS layout which I've produced with Photoshop/Dreamweaver especially for my ebay auctions. This is what it looks like...
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.