这个可能是编译器的问题吧。我用的VC++2005……VC6对C++的标准实现不好……还有,就是static const是可以在里面也可以在外面声明的,但是如果在外面,就必须这么写:
template<class T>
const T A<T>::v3=1;
因为V3是个常量变量……我昨天编译失败就是这个原因,这样就不会出问题了。记住,VC6对C++的实现是不完整的。这里再引用《C++ Primer》里面的一段话:
Ordinarily, class static members, like ordinary data members, cannot be initialized in the class body. Instead, static data members are normally initialized when they are defined.
One exception to this rule is that a const static data member of integral type can be initialized within the class body as long as the initializer is a constant expression:
class Account {
public:
static double rate() { return interestRate; }
static void rate(double);
// sets a new rate
private:
static const int period = 30; // interest posted every 30 days
这里,static const可以直接在类内部定义,其余情况都必须在外部实现,另外,static成员的定义必须放在CPP文件中,防止重复声明
double daily_tbl[period]; // ok: period is constant expression
};
A const static data member of integral type initialized with a constant value is a constant expression. As such, it can be used where a constant expression is
required, such as to specify the dimension for the array member daily_tbl.
When a const static data member is initialized in the class body, the data member must still be defined outside the class definition.
When an initializer is provided inside the class, the definition of the member must not specify an initial value:
// definition of static member with no initializer;
// the initial value is specified inside the class definition
const int Account::period;