首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

const类型声明有关问题

2012-02-05 
const类型声明问题头文件#includeiostreamusingnamespacestdclassGamePlayer{public:GamePlayer()intg

const类型声明问题
头文件
#include   <iostream>
using   namespace   std;

class   GamePlayer
{
public:
GamePlayer();
int   getConst();
private:
static   const   int   NUM;
}

cpp文件
#include   "headwang1.h "

static   const   int   GamePlayer::NUM=5;

int   GamePlayer::getConst()
{
return   NUM;
}

void   main()
{
GamePlayer   g;
cout < <g.getConst() < <endl;
}

报的错:
D:\MyProject\wang\cppwang1.cpp(3)   :   error   C2628:   'GamePlayer '   followed   by   'int '   is   illegal   (did   you   forget   a   '; '?)
D:\MyProject\wang\cppwang1.cpp(3)   :   error   C2371:   'private:   static   int   const   GamePlayer::NUM '   :   redefinition;   different   basic   types
                d:\myproject\wang\headwang1.h(10)   :   see   declaration   of   'private:   static   int   const   GamePlayer::NUM '

请高人指点迷津

[解决办法]
1. 类结束时没分号。
2. CPP 中定义的时候不要加 static 了: const int GamePlayer::NUM=5;

热点排行