Partial template specialization

M

Martin

I'm trying to make a partial specialization of a class of mine. Can
someone please tell me what's wrong with the following code? GCC gives
me the error "invalid use of undefined type "class X<int, B>"".

template <class A, class B>
class X
{
public:
void f();
};

template <>
void X<int, int>::f()
{ cout << "int-int";}

template <class B>
void X<int, B>::f() //error on this line
{ cout << "int-B"; }

int main( int argc, char** argv )
{
X<int, double> x;
x.f();
return 0;
}
 
V

Victor Bazarov

Martin said:
I'm trying to make a partial specialization of a class of mine.

No, you are not. You are confused.
Can
someone please tell me what's wrong with the following code? GCC gives
me the error "invalid use of undefined type "class X<int, B>"".

template <class A, class B>
class X
{
public:
void f();
};

template <>
void X<int, int>::f()
{ cout << "int-int";}

template <class B>
void X<int, B>::f() //error on this line
{ cout << "int-B"; }

The preceding definition (of 'X<int,B>::f') is NOT a partial specialisation
of the class template X. It's a definition of a member of the
*non-existent*
partial specialisation of 'X'. And that's prohibited. If you need to
define
a partial specialisation of 'X', do exactly that:

template<class B>
class X<int, B> {
public:
void f();
};

and *only then* define the member.
int main( int argc, char** argv )

Don't declare arguments you're not going to use.
{
X<int, double> x;
x.f();
return 0;
}

V
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top