求教一个关于静态数组的问题
在类中有一个静态数组
class Myclass{
public:
static int p[3];
};
在类外定义如下
int Myclass::p[0]=1;
int Myclass::p[1]=2;
int Myclass::p[2]=3;
为什么会出错? 问题在哪里?
c:\console\myclass.cpp(3) : error C2466: cannot allocate an array of constant size 0
c:\console\myclass.cpp(3) : error C2440: 'initializing ' : cannot convert from 'const int ' to 'int [3] '
There are no conversions to array types, although there are conversions to references or pointers to arrays
c:\console\myclass.cpp(4) : error C2369: 'public: static int * Myclass::p ' : redefinition; different subscripts
c:\console\myclass.h(4) : see declaration of 'public: static int * Myclass::p '
c:\console\myclass.cpp(5) : error C2369: 'public: static int * Myclass::p ' : redefinition; different subscripts
c:\console\myclass.h(4) : see declaration of 'public: static int * Myclass::p '
[解决办法]
int Myclass::p[0]=1;
int Myclass::p[1]=2;
int Myclass::p[2]=3;
类里只是声明 类外才是定义
这样写的话 编译器当作 定义一个0个元素的int数组叫p 又定义一个1个元素的数组p redefinition