Khi tôi cố gắng sử dụng floatlàm tham số mẫu, trình biên dịch sẽ kêu mã này, trong khi inthoạt động tốt.
Có phải vì tôi không thể sử dụng floatlàm tham số mẫu không?
#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;
}
Lỗi:
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'
Tôi đang đọc "Cấu trúc dữ liệu cho lập trình viên trò chơi" của Ron Penton, tác giả vượt qua a float, nhưng khi tôi thử thì nó dường như không biên dịch.
floatlàm tham số mẫu không phải kiểu không? Chương nào vậy?