c++ 静态变量 编译时出错? 400 报错
mpai.h
#ifndef MPAI_H
#define MPAI_H
#include <iostream>
#include <time.h>
#include <stdlib.h>
#define M_MAXCOUNT 112
class MPai
{
private:
// short Pai[112];
static short CrrIndex;
static short CrrPai[M_MAXCOUNT];
static void Start();
public:
MPai();
static void MReadm();
static short GetNextPai();
//static short
};
//short MPai::CrrPai[]={0};
//short MPai::CrrIndex=0;
#endif // MPAI_H
mpai.cpp
#include "mpai.h"
MPai::MPai()
{
}
void MPai::Start()
{
for(short i=0;i<M_MAXCOUNT;i++)
CrrPai[i]=i;
CrrIndex=0;
}
void MPai::MReadm()
{
Start();
short itemp=-1,rtemp=-1;
for(short i=0;i<M_MAXCOUNT;i++)
{
itemp = CrrPai[i];
srand(time(NULL));
rtemp = rand()%M_MAXCOUNT;
CrrPai[i]=CrrPai[rtemp];
CrrPai[rtemp]=itemp;
// CrrPai[i]=Pai[i];
}
}
short MPai::GetNextPai()
{
if(CrrIndex<M_MAXCOUNT)
return CrrPai[CrrIndex++];
else
return -1;
}
环境 mingw
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:9: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:10: undefined reference to `MPai::CrrIndex'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:18: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:21: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:21: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:22: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:28: undefined reference to `MPai::CrrIndex'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:29: undefined reference to `MPai::CrrIndex'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:29: undefined reference to `MPai::CrrPai'
debug/mpai.o:F:\qtdev\test-build-desktop/../test/mpai.cpp:29: undefined reference to `MPai::CrrIndex'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug/test.exe] Error 1
mingw32-make: *** [debug] Error 2
静态变量没有初始化,你头文件中最下面的注释掉的几句
//short MPai::CrrPai[]={0};
//short MPai::CrrIndex=0;
去年注释看看。
######楼上正解,静态成员没有初始化
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。