template member function of non-template class

R

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 T> void 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 T> void fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class T> void 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
 
V

Victor Bazarov

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 T> void 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 T> void fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class T> void 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
 
A

amparikh

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 T> void 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 T> void fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class T> void 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.
 
M

mlimber

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 T> void 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 T> void fn(T* ptr);
};
#endif /* _XYZ_H_ */

(xyz.cpp)
#include "xyz.h"
template <class T> void 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-lite/templates.html#faq-35.14), and
with that compiler, you probably need to define it inline in the
definition of the class.

Cheers! --M
 
R

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top