473,385 Members | 1,372 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,385 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 1693
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.