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;