为什么我不能使用浮点值作为模板参数?
当我尝试float用作模板参数时,编译器会为此代码而烦恼,同时int工作正常。
是因为我不能float用作模板参数吗?
#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; }错误:
main.cpp: In function `int main()': main.cpp:25: error: `float' is not a valid type for a template constant parameter main.cpp:25: error: invalid type in declaration before ';' token main.cpp:28: error: request for member `returnVal' in `gcFlaot', which is of non-class type `int'
我正在阅读Ron Penton撰写的“游戏程序员的数据结构”,作者传递了一个float,但是当我尝试它时似乎没有编译。
凤凰求蛊
千万里不及你
回首忆惘然
慕标5832272
随时随地看视频慕课网APP
相关分类