Connecting Tech Pros Worldwide Forums | Help | Site Map

Doesn't Compile, Why?

liam_herron
Guest
 
Posts: n/a
#1: Jun 27 '08

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



liam_herron
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Doesn't Compile, Why?


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

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Doesn't Compile, Why?


On 2008-04-22 21:55, liam_herron wrote:
Quote:
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
liam_herron
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Doesn't Compile, Why?


This compiled on Windows but not on Linux?

Why do I need to specify the "typename"?

Ahh, template syntax is always so straightforward :)



sk_usenet
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Doesn't Compile, Why?



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

--
http://techytalk.googlepages.com


Joe Greer
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Doesn't Compile, Why?


liam_herron <liam_herron@hotmail.comwrote in news:e4d15259-3bb6-416b-
8fe9-b4190c435eff@m36g2000hse.googlegroups.com:
Quote:
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.
Quote:
>
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.
Quote:
>
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
Joe Greer
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Doesn't Compile, Why?


Joe Greer <jgreer@doubletake.comwrote in
news:Xns9A895D01F49Djgreerdoubletakecom@194.177.96 .78:
Quote:
liam_herron <liam_herron@hotmail.comwrote in
news:e4d15259-3bb6-416b-
8fe9-b4190c435eff@m36g2000hse.googlegroups.com:
>
Quote:
>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...
Quote:
>
joe
Closed Thread