you're close enough. I think you have the general idea right, but just not the syntax (which can be pretty scary at first!!)
ok in your function you have
Code:
template <class Value>
void printAbsValue()
{
//snip
}
which should be
Code:
template <typename Value> // 1
void printAbsValue(Value v) // 2
{
// 3
if (v<0)
{
cout<<"The absolute value of "<<v<<" is "<<(-v)<<endl;
}
else
{
cout<<"The absolute value of "<<v<<" is "<<v<<endl;
}
}
1. this is a nit, but you should really use typename instead of class.
2. ok you've declared the template parameter, but you still need to pass an instance of the templated type to the function
3. use the instance of the templated type in the body of the function
ok now for you client code
Code:
int a[aValue]=-23;
float b[bValue]=-23.55;
double c[cValue]=-2.4;
no idea what you thought you were doing here but it's not what you want. (you're actually declaring an array here)
Code:
printAbsValue(a, aValue);
printAbsValue(b, bValue);
printAbsValue(c, cValue);
ok again you're close here, but not quite
syntax is actually
Code:
printAbsValue<int>(a);
printAbsValue<float>(b);
printAbsValue<double>(c);
// but a good compiler will deduce the arguements so it's just
printAbsValue(a);
printAbsValue(b);
printAbsValue(c);
hope that helps