When I try to use float as a template param, the compiler cries for this code, while int works fine. Is it that I cannot use float as a template parameter?
#include<iostream>
using namespace std;
template <class T, T defaultValue>
class GenericClass
{
private:
T value;
public:
GenericClass()
{
value = defaultValue;
}
T returnVal()
{
return value;
}
};
int main()
{
GenericClass <int, 10> gcInteger;
GenericClass < float, 4.6f> gcFlaot;
cout << "\n sum of integer is "<<gcInteger.returnVal();
cout << "\n sum of float is "<<gcFlaot.returnVal();
return 0;
}
Error:
somthing.cpp: In function `int main()':
somthing.cpp:25: error: `float' is not a valid type for a template constant parameter
somthing.cpp:25: error: invalid type in declaration before ';' token
somthing.cpp:28: error: request for member `returnVal' in `gcFlaot', which is of non-class type `int'
I am reading "Data Structures for Game Programmers" by Ron Penton, the author passes a float, while when I try it it doesn't seem to compile.