template <>
class pr<int>{
int value1, value2;
public:
pr (int first, int second)
{value1=first; value2=second;}
int module ();
};
template <>
int pr<int>::module() {
return value1%value2;
}
invalid explicit specialization of 'pr<int>::module()' etc
you don't need a template here if it is always going to be an int returned. I don't think you want the modulus of two chars, etc. You may want to make a template if your going to use numeric types other than ints such as long, if that is the case just use the regular template syntax. And since % only works on integral type int, long, char, unsigned char... a template won't work very well. Such as a double won't work with the % operator.
Code:
class pr
{
int value1, value2;
public:
pr (int first = 1, int second = 1)
{value1=first; value2=second;}
int module ();
};
int pr::module() {
return value1%value2;
}
if you want a template your original ( with initialization in constructor which causes problems with some data type obviously) :
Code:
template <class T>
class pr {
T value1, value2;
public:
pr (T first = 1, T second = 1)
{value1=first; value2=second;}
T module () {value1 % value2;}
};
pr <int> nPr;
pr<char> cPr; //will work not much value
pr<string> sPr; //will not work the constructor cannot use the string template becuase of the constructor uses int
pr<double> d1( 10, 4 );//will work
d1.module( );//fails on compile % is not an operator for double
Basically this is not a good use of a template, its not needed here.
You'll also have to do more checking in your module method to avoid dividing by zero. Bad Ju Ju...
dang