473,395 Members | 1,442 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,395 software developers and data experts.

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 6329
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...@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?

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
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...
20
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...
13
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...
30
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
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: ...
4
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...
7
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...
4
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:...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
13
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...
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: 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
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...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.