I went through my book's chapter about function templates and I'm trying to make sure I understand this correctly.
There’s an example in the book so I tried to do a program using templates to create a program on absolute values to see If I get it, but I’m getting a bunch of errors so obviously I didn’t.
Here’s what I did:
Here are the errors I’m getting:Code:#include<iostream> using namespace std; template <class Value> void printAbsValue() { if (Value<0) { cout<<"The absolute value of "<<Value<<" is "<<(-Value)<<endl; } else { cout<<"The absolute value of "<<Value<<" is "<<Value<<endl; } } int main() { const int aValue=10; const float bValue=10.4; const double cValue=1.5; int a[aValue]=-23; float b[bValue]=-23.55; double c[cValue]=-2.4; printAbsValue(a, aValue); printAbsValue(b, bValue); printAbsValue(c, cValue); return 0; }
Can anyone tell me if I understand this concept correctly and point me in the right direction to understand this.Code:.cpp(28) : warning C4305: 'initializing' : truncation from 'double' to 'const float' .cpp(31) : error C2440: 'initializing' : cannot convert from 'int' to 'int [10]' There are no conversions to array types, although there are conversions to references or pointers to arrays .cpp(32) : error C2057: expected constant expression .cpp(32) : error C2466: cannot allocate an array of constant size 0 .cpp(32) : error C2440: 'initializing' : cannot convert from 'double' to 'float []' There are no conversions to array types, although there are conversions to references or pointers to arrays .cpp(33) : error C2057: expected constant expression .cpp(33) : error C2466: cannot allocate an array of constant size 0 .cpp(33) : error C2440: 'initializing' : cannot convert from 'double' to 'double []' There are no conversions to array types, although there are conversions to references or pointers to arrays .cpp(35) : error C2780: 'void printAbsValue(void)' : expects 0 arguments - 2 provided .cpp(12) : see declaration of 'printAbsValue' .cpp(36) : error C2780: 'void printAbsValue(void)' : expects 0 arguments - 2 provided .cpp(12) : see declaration of 'printAbsValue' .cpp(37) : error C2780: 'void printAbsValue(void)' : expects 0 arguments - 2 provided .cpp(12) : see declaration of 'printAbsValue'
Thanks.



LinkBack URL
About LinkBacks



Want to add some