Doesn't Compile, Why?

L

liam_herron

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
 
L

liam_herron

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
 
L

liam_herron

This compiled on Windows but not on Linux?

Why do I need to specify the "typename"?

Ahh, template syntax is always so straightforward :)
 
S

sk_usenet

liam_herron said:
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.
 
J

Joe Greer

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
 
J

Joe Greer

(e-mail address removed):


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top