473,320 Members | 2,202 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.

template member function

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);
}

Oct 15 '05 #1
5 1691
Pe*******@gmail.com wrote:
void doit(typename B<T> &b){//error
std::cout << "in " << std::endl;
}


Take out the "typename" keyword.

Jacques.
Oct 15 '05 #2
Pe*******@gmail.com wrote:
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<T> &b) { // no "typename" here
std::cout << "in " << std::endl;
}
};

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


V
Oct 15 '05 #3
Victor Bazarov wrote:
"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.
Oct 15 '05 #4
Jacques Labuschagne wrote:
Victor Bazarov wrote:
"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.


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

Oct 15 '05 #5
In article <11**********************@z14g2000cwz.googlegroups .com>,
Greg <gr****@pacbell.net> wrote:
Jacques Labuschagne wrote:
Victor Bazarov wrote:
> "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.


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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #6

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

Similar topics

0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
1
by: amparikh | last post by:
I have something like this. typedef enum TYPES{ X =0, Y, Z, MAX}; template <typename T> class A { public:
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
4
by: James Aguilar | last post by:
Guys, When I specialize a template class member function (I.e. a member function of a template class) based on that class' type, bad things happen. Here's some code: ---- test_header.h...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.