开发者社区> 问答> 正文

c++ 静态变量 编译时出错? 400 报错

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

展开
收起
爱吃鱼的程序员 2020-05-30 22:25:43 549 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

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

    去年注释看看。

    ######

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

    2020-05-30 22:25:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载