Problem with multiple parameter templates and function overloading



 DEVELOP > c-Plus-Plus > Problem with multiple parameter templates and function overloading

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 12 Sep 2006 10:43:32 AM
Object: Problem with multiple parameter templates and function overloading
I have written the following very short template class in testclass.h:
template<typename C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
};
template<typename C,typename T>
T TestClass<C,T>::testFunction(T dummy)
{
C bla;
return dummy;
}
So far it compilates fine.
Now i want to overload the testFunction:
template<typename C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
/* definition of overloaded function */
double testFunction(double dummy);
};
template<typename C,typename T>
T TestClass<C,T>::testFunction(T dummy)
{
C bla;
return dummy;
}
/* now comes the problematic part */
template<typename C>
double TestClass<C,double>::testFunction(double dummy)
{
C bla;
return dummy;
}
I get the following compiler error:
behavior/testclass.h:47: error: no `double TestClass<C,
double>::testFunction(double)' member function declared in class `
TestClass<C, double>'
behavior/testclass.h:47: error: template definition of non-template
`double
TestClass<C, double>::testFunction(double)'
If I remove the parameter C everything works fine of course.
Please can anybody help me.
I think its probably just a small problem.
.

User: ""

Title: Re: Problem with multiple parameter templates and function overloading 12 Sep 2006 12:28:49 PM
wrote:

I have written the following very short template class in testclass.h:

template<typename C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
};

template<typename C,typename T>
T TestClass<C,T>::testFunction(T dummy)
{
C bla;
return dummy;

}

So far it compilates fine.
Now i want to overload the testFunction:

template<typename C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
/* definition of overloaded function */
double testFunction(double dummy);
};

template<typename C,typename T>
T TestClass<C,T>::testFunction(T dummy)
{
C bla;
return dummy;

}

/* now comes the problematic part */

template<typename C>
double TestClass<C,double>::testFunction(double dummy)
{
C bla;
return dummy;
}

Your testFunction is not a template function. I am not quite sure what
you are trying to do, but the bottom line is you have a template class.
So, if you want to have a specialized testfunction implementation for
double, then first specialize the template class TestClass for double
and then have an implementation for that.
Look up partial specialization (function templates cannot support them)
something like this...
template<typename C>
class TestClass<C, double>
{
public:
double testFunction(double dummy);
};
template<typename C>
double TestClass<C,double>::testFunction(double dummy)
{
C bla;
return dummy;
}


I get the following compiler error:

behavior/testclass.h:47: error: no `double TestClass<C,
double>::testFunction(double)' member function declared in class `
TestClass<C, double>'
behavior/testclass.h:47: error: template definition of non-template
`double
TestClass<C, double>::testFunction(double)'

If I remove the parameter C everything works fine of course.
Please can anybody help me.
I think its probably just a small problem.

.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER