c++ 静态变量 编译时出错

一个桔子 发布于 2010/12/31 11:41
阅读 756
收藏 1

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

加载中
0
晓寒
晓寒

静态变量没有初始化,你头文件中最下面的注释掉的几句

//short MPai::CrrPai[]={0};
//short MPai::CrrIndex=0;

去年注释看看。

0
C**
C**

楼上正解,静态成员没有初始化

返回顶部
顶部