cpp-static 发表于 2018-09-30 分类于 c++ 阅读次数: 本文字数: 242 阅读时长 ≈ 1 分钟 类静态成员变量存取出错 undefined reference to ‘xxx’#define A_Hclass A{ static int num1; //这里只是声明,并没有定义 int num2;//声明,在实例化时分配内存 A();}#endif#include "a.h"int A::mun1;//定义,分配内存,以后A每一个对象(实例)的创建都不再分配内存void A::A(){ num2=1;//正确 num1=2;//没有上面的定义会报错}