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

Doesn't Compile, Why?


Any idea why the following code doesn't compile?

#include <iostream>
#include <vector>

class A
{
public:

template<typename T>
void f( const std::vector<T>& v)
{
std::vector<T>::const_iterator it = v.begin();
for (, it != v.end(); ++it)
{
std::cout << *it << std::endl;
}
}

private:

};

--------------------------------------------------------------------------------------------------------------------------------
Compilation Errors:

testVectorIteratorInClass.cpp: In member function `void A::f(const
std::vector<T, std::allocator<_CharT&)':
testVectorIteratorInClass.cpp:16: error: expected `;' before "it"
testVectorIteratorInClass.cpp:17: error: expected primary-expression
before ',' token
testVectorIteratorInClass.cpp:17: error: `it' was not declared in this
scope
testVectorIteratorInClass.cpp:17: error: expected `;' before ')' token
Jun 27 '08 #1
6 1679
Sorry, typo, it was actually:

#include <iostream>
#include <vector>

class A
{
public:

template<typename T>
void f( const std::vector<T>& v)
{
std::vector<T>::const_iterator it = v.begin();
for (; it != v.end(); ++it)
{
std::cout << *it << std::endl;
}
}

private:

};

With the following errors:
-------------------------------------------------------------------
testVectorIteratorInClass.cpp: In member function `void A::f(const
std::vector<T, std::allocator<_CharT&)':
testVectorIteratorInClass.cpp:16: error: expected `;' before "it"
testVectorIteratorInClass.cpp:17: error: `it' was not declared in this
scope

Jun 27 '08 #2
On 2008-04-22 21:55, liam_herron wrote:
Any idea why the following code doesn't compile?

#include <iostream>
#include <vector>

class A
{
public:

template<typename T>
void f( const std::vector<T>& v)
{
std::vector<T>::const_iterator it = v.begin();
Replace the above with

typename std::vector<T>::const_iterator it = v.begin();

See also http://www.parashift.com/c++-faq-lit...html#faq-35.18

--
Erik Wikström
Jun 27 '08 #3
This compiled on Windows but not on Linux?

Why do I need to specify the "typename"?

Ahh, template syntax is always so straightforward :)

Jun 27 '08 #4

"liam_herron" <li*********@hotmail.comwrote in message
This compiled on Windows but not on Linux?
Who said compilers are correct all the time :) ?
Why do I need to specify the "typename"?
Because you are dealing with a dependent name. Read the FAQ link given to
you earlier.
Ahh, template syntax is always so straightforward :)

--
http://techytalk.googlepages.com
Jun 27 '08 #5
liam_herron <li*********@hotmail.comwrote in news:e4d15259-3bb6-416b-
8f***************@m36g2000hse.googlegroups.com:
This compiled on Windows but not on Linux?
I assume that when you say Windows you mean VC and Linux you mean gcc.
OS's in general don't compile much of anything. But anyway, VC does use a
strictly conforming 2 phase lookup model which in some cases (like this) is
good and others is not.
>
Why do I need to specify the "typename"?
You need the typename to tell the compiler that you expect that syntax to
result in a type. Without it, it can't tell until you instantiate the
method with some type. Remember that templates can be specialized and each
specializaton can look pretty radically different from any other. Of
course, in this case, since it is a std container, the compiler could know,
but in general it doesn't.
>
Ahh, template syntax is always so straightforward :)
Ahhh, I love a good joke. I think the problem with templates is that the
went from a simple mechanism for writing generics to a programming language
all its own and the syntax has suffered. But that's just my opinion.

joe
Jun 27 '08 #6
Joe Greer <jg****@doubletake.comwrote in
news:Xn********************************@194.177.96 .78:
liam_herron <li*********@hotmail.comwrote in
news:e4d15259-3bb6-416b-
8f***************@m36g2000hse.googlegroups.com:
>This compiled on Windows but not on Linux?

I assume that when you say Windows you mean VC and Linux you mean gcc.
OS's in general don't compile much of anything. But anyway, VC does
use a strictly conforming 2 phase lookup model which in some cases
(like this) is good and others is not.
Hmmm, I meant to say doesn't use a conforming 2 phase lookup...
>
joe
Jun 27 '08 #7

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

Similar topics

0
by: James Thurley | last post by:
I'm trying to dynamically compile assemblies and cache them to disk, which seems to work fine. When the data I'm compiling from changes, I want to re-generate the assembly and use the new version....
4
by: Fernando Cuenca | last post by:
Hi, I'm trying to explicitly instantiate a template function using the following syntax: obj.template_func<type>(params); It compiles OK when used from a regular function, but it doesn't...
12
by: Rhino | last post by:
I am having an odd problem: the sqlj command on my system doesn't work. I am running DB2 (LUW) V8 (FP8) on WinXP. I haven't done an sqlj program since Version 6 of DB2 (LUW) so I checked the...
3
by: Karl Irvin | last post by:
When I click Debug>Compile, my modules seem to comple OK but the Compile menu choice doesn't gray out (to indicate compiling is not needed). The IsCompiles returns false, no matter how many times...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
1
by: rvan | last post by:
Hi, I am trying to port one of my older MFC APP created in VC 6.0 to VC 7.0. Right away after starting the compile process, I get following errors and compilation stops. "d:\Microsoft .NET...
4
by: Eric Lilja | last post by:
Is this an invalid program? Doesn't compile on my system: #include <cstdio> class Why { enum TArch {LITTLE_ENDIAN, BIG_ENDIAN, NON_IEEE}; TArch Architecture; }; int
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
35
by: mwelsh1118 | last post by:
Why doesn't C# allow incremental compilation like Java? Specifically, in Java I can compile single .java files in isolation. The resulting individual .class files can be grouped into .jar files....
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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.