473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1712
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************ **********@z14g 2000cwz.googleg roups.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
1643
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 internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the fault. It mostly works fairly well. That catch being that we have to hand implement some of the code...
7
2130
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
6589
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
1665
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
2317
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 want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
4
4065
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 #ifndef TEST_HEADER_H #define TEST_HEADER_H
3
2921
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
12
1877
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 to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
5
2269
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
2
7698
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
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.