template member function

P

PengYu.UT

I can't figure out what is wrong with following program. Would you
please help me? The error line is marked with error.

Peng

#include <iostream>

template <typename T>
class B{
public:
T t;
};

class A{
public:
template <typename T>
void doit(typename B<T> &b){//error
std::cout << "in " << std::endl;
}
};

int main(int argc, char *argv[])
{
B<int> b;
A a;
a.doit<int>(b);
}
 
V

Victor Bazarov

I can't figure out what is wrong with following program. Would you
please help me? The error line is marked with error.

Peng

#include <iostream>

template <typename T>
class B{
public:
T t;
};

class A{
public:
template <typename T>
void doit(typename B<T> &b){//error

"error". Which error? Does your compiler just say "error" or
does it actually try to explain what it thinks is wrong?

Anyway, my compiler compiles it without a problem. It, of course
doesn't mean the code is fine. You need to remove the second
'typename' in this declaration. The line should be

void doit(B said:
std::cout << "in " << std::endl;
}
};

int main(int argc, char *argv[])
{
B<int> b;
A a;
a.doit<int>(b);
}

V
 
J

Jacques Labuschagne

Victor said:
"error". Which error? Does your compiler just say "error" or
does it actually try to explain what it thinks is wrong?

My compiler could certainly have been more helpful, IMHO. GCC 3.3 says
foo.cc:12: error: parse error before `&' token
It was good enough for me, but I can understand PengYu's confusion.

Jacques.
 
G

Greg

Jacques said:
My compiler could certainly have been more helpful, IMHO. GCC 3.3 says
foo.cc:12: error: parse error before `&' token
It was good enough for me, but I can understand PengYu's confusion.

But imagine the compiler's confusion to run into the "typename" keyword
in what it thought was going to be a method declaration. At that point
the compiler has no idea what the programmer is trying to write, so it
has no idea which part of the expression the programmer should remove.

Greg
 
G

Greg Comeau

But imagine the compiler's confusion to run into the "typename" keyword
in what it thought was going to be a method declaration. At that point
the compiler has no idea what the programmer is trying to write, so it
has no idea which part of the expression the programmer should remove.

You told it typename. It is believe you:

"ComeauTest.c", line 17: error: a class or namespace qualified name is required
void doit(typename B<T> &b){//error

(as per the rules of typename) or it doesn't.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top