473,657 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template member function of non-template class

rf
As I understand it, it is legal to have a template member function of a
non-template class, as in the following example:

class XYZ
{
public:
template <class Tvoid fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}
};

This example compiles and works as expected. However, if I move the
member function definition out of the class definition as follows:

(xyz.h)
#ifndef _XYZ_H_
#define _XYZ_H_
class XYZ
{
public:
template <class Tvoid fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class Tvoid XYZ::fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}

Then MS Visual C++ 5.0 gives me the error "error C2660: 'fn' : function
does not take 1 parameters" on the line I invoke the function from as
shown below:

int ix = 43;
XYZ xyz;
xyz.fn(&ix);

Is this a compiler bug, or have I overlooked something?

Thanks,
BF

Nov 21 '06 #1
5 6348
rf@volcanomail. com wrote:
As I understand it, it is legal to have a template member function of
a non-template class, as in the following example:

class XYZ
{
public:
template <class Tvoid fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}
};
Yes.
This example compiles and works as expected. However, if I move the
member function definition out of the class definition as follows:

(xyz.h)
#ifndef _XYZ_H_
#define _XYZ_H_
class XYZ
{
public:
template <class Tvoid fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class Tvoid XYZ::fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}

Then MS Visual C++ 5.0
O!M!G! (using the gestures and the voice of the Janice character
from "Friends"). . What do you expect? This is a compiler that
is, like, fifty years old! Get a decent compiler.
gives me the error "error C2660: 'fn' :
function does not take 1 parameters" on the line I invoke the
function from as shown below:

int ix = 43;
XYZ xyz;
xyz.fn(&ix);

Is this a compiler bug, or have I overlooked something?
Most likely a compiler bug. Nonetheless... Read the FAQ about
templates. It should discourage you from placing templates in
a separate file.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '06 #2

r...@volcanomai l.com wrote:
As I understand it, it is legal to have a template member function of a
non-template class, as in the following example:

class XYZ
{
public:
template <class Tvoid fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}
};

This example compiles and works as expected. However, if I move the
member function definition out of the class definition as follows:

(xyz.h)
#ifndef _XYZ_H_
#define _XYZ_H_
class XYZ
{
public:
template <class Tvoid fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class Tvoid XYZ::fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}

Then MS Visual C++ 5.0 gives me the error "error C2660: 'fn' : function
does not take 1 parameters" on the line I invoke the function from as
shown below:

int ix = 43;
XYZ xyz;
xyz.fn(&ix);

Is this a compiler bug, or have I overlooked something?

Thanks,
BF
First of MS VC++ 5.0 is a very old compiler and you should upgrade
yourself immediately.
The old compilers had pretty substandard support for templates.
If you cannot upgrade, then try playing around with template arg
deduction for the function call by specifying explicit templ
arguements.
I am just speculating here as I cannot see the errors.

Finally, if your template function implementation is in xyz.cpp and the
call is from another file(say main.cpp), I am pretty sure you would be
getting a linker error unless of course you have done the "right thing"
by explicitly instantiating the template function at the end of the
header file.

Nov 21 '06 #3
rf@volcanomail. com wrote:
As I understand it, it is legal to have a template member function of a
non-template class, as in the following example:

class XYZ
{
public:
template <class Tvoid fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}
};

This example compiles and works as expected. However, if I move the
member function definition out of the class definition as follows:

(xyz.h)
#ifndef _XYZ_H_
#define _XYZ_H_
class XYZ
{
public:
template <class Tvoid fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class Tvoid XYZ::fn(T* ptr)
{
T val = *ptr;
cout << val << endl;
}

Then MS Visual C++ 5.0 gives me the error "error C2660: 'fn' : function
does not take 1 parameters" on the line I invoke the function from as
shown below:

int ix = 43;
XYZ xyz;
xyz.fn(&ix);

Is this a compiler bug, or have I overlooked something?
This is almost certainly a bug. VC++ 5.0?! What do you expect with a
compiler so old?

You need to define the function in the header (as you must with nearly
every compiler; cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), and
with that compiler, you probably need to define it inline in the
definition of the class.

Cheers! --M

Nov 21 '06 #4
rf
Thanks for the advice. The real function I'm using is rather large, so
I wanted to get it out of the class definition if I could. Guess that's
not an option.

By the way, due to factors beyond my control, VC++ 5.0 is my only
option.

Thanks,
BF
>
This is almost certainly a bug. VC++ 5.0?! What do you expect with a
compiler so old?

You need to define the function in the header (as you must with nearly
every compiler; cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), and
with that compiler, you probably need to define it inline in the
definition of the class.

Cheers! --M
Nov 21 '06 #5

rf@volcanomail. com wrote:
Thanks for the advice. The real function I'm using is rather large, so
I wanted to get it out of the class definition if I could. Guess that's
not an option.

By the way, due to factors beyond my control, VC++ 5.0 is my only
option.

Thanks,
BF

This is almost certainly a bug. VC++ 5.0?! What do you expect with a
compiler so old?

You need to define the function in the header (as you must with nearly
every compiler; cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), and
with that compiler, you probably need to define it inline in the
definition of the class.

Cheers! --M
check http://msdn.microsoft.com/vstudio/express/visualc/

Nov 22 '06 #6

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

Similar topics

2
6521
by: srinivas reddy | last post by:
Hi, Can a constant member function be overloaded with its non-constant equivalent. I mean can void func() be overloaded with void func() const. If so what behaviour is guaranteed, how does the compiler resolve ambiguity?, and how could I call a specific variant? tia, Srinivas
20
8369
by: Tim Martin | last post by:
Hi, I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me a bit. The author states: 'C++ has no "methods." Java and Smalltalk have methods. When you talk about an object-oriented design and are feeling particularly pretentious, you may use the terms "message" and "method," but when you get down to discussing a C++ implementation of your design, use the terms "function call" and "member function."'
13
2427
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed - http://www.codeguru.com/cpp/tic/tic0129.shtml) "In general, if it doesn't make any difference, they should be members, to emphasize the association between the operator and its class." That seems...
30
3470
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
7
1548
by: James Vanns | last post by:
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an external C struct (external to the C++ project/classes etc.) which is wants a function ptr assigned to one of it's members: struct blah { int (func_ptr*) (int a, int b, float c); }; etc... However, my code is in C++ and a wish to assign a non-static member function (I know I can do this) to this struct member! Can this be
4
1871
by: Howard | last post by:
Hi, I came across some sample code for using a third-party API, and the code in it had several free-standing (i.e., non-member) functions, but strangely, these were declared as 'static'. My compiler tells me that the function has no prototype, which is weird, because there *is* a prototype in the header file. I was not able to find in my books any reference to a static function that was not a member function. The closest info I could...
7
2487
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
4
2374
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public: void start(); void run();
14
4194
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
13
2517
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
0
8420
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
8324
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
8842
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
8740
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
6176
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
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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.