据我所知,模板值需要在编译时间内知道。 所以我写了一个小例子来看看我明白了,但是很明显我没有。 ,所以我得到这样的:模板值'defValue'不能出现在常量表达式中
`defValue' cannot appear in a constant-
expression
谁能请什么问题以及如何修复?
#include <iostream>
template <class T,T defaultVal, int dim=255>
class Vec
{
T _vec[dim];
int _dim;
public:
Vec() : _dim(dim)
{
for (int i=0;i<_dim;++i)
{
_vec[i] = defaultVal;
}
}
~Vec() {};
// other operators and stuff
};
int main()
{
int defValue = 0;
Vec < int,defValue > vecWithDefVal;// here is the problem but i don't know why
}
'const int的利用DefValue = 0;'不VC++或GCC下工作。 – 2010-10-02 10:31:49
[应该可以](http://ideone.com/G1ZpC)。它也适用于我的MSVS :) – 2010-10-02 10:34:01
奇怪的是,我必须引入一个错字。所以是的,它确实有效! – 2010-10-02 10:36:25