function templates

H

Helge Kruse

Hello,

I want to implement a function template separated from the
declaration:

class X
{
public:
template<class T> void operator << (T);
};

template<class T> void X::eek:perator << (T t)
{
}

I got the error:
binary '<<' : 'class X' does not define this operator or a conversion
to a type acceptable to the predefined operator

When I implement the function inside the class X, everything is OK.
How can this be solved, when the function "operator <<(T)" must be
outside of the class X?

Compiler is Microsoft Visual C++ for Windows CE 3.0.

Regards,
Helge
 
G

Gernot Frisch

Helge Kruse said:
Hello,

I want to implement a function template separated from the
declaration:

class X
{
public:
template<class T> void operator << (T);
};

template<class T> void X::eek:perator << (T t)
{
}

I got the error:
binary '<<' : 'class X' does not define this operator or a
conversion
to a type acceptable to the predefined operator

When I implement the function inside the class X, everything is OK.
How can this be solved, when the function "operator <<(T)" must be
outside of the class X?

Compiler is Microsoft Visual C++ for Windows CE 3.0.

Regards,
Helge

The template class definition must be in the header file. Or include
an .inc file in the header.
 
R

Rob Williscroft

Helge Kruse wrote in in comp.lang.c++:

Compiler is Microsoft Visual C++ for Windows CE 3.0.

That's you problem, your code is perfectly good C++, but
some Microsoft compilers do like template members to be
*defined* inside the body of the class.

Workaround for non-inline:

struct X;
template < typename T > void op_shift_left( X *, T );

struct X
{
template < typename T >
void operator << ( T t )
{
op_shift_left( this, t );
}
};

template < typename T >
void op_shift_left( X *that, T t )
{
// real *non-inline* defenition here.
}

HTH.

Rob.
 

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

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,528
Latest member
AnaHawley8

Latest Threads

Top