设置静态成员指针变量

时间:2010-04-12 23:45:55

标签: c++ static-variables class-variables

我正在尝试在类中设置静态指针变量,但是我为我尝试设置的每个变量收到了这些错误。

错误C4430:缺少类型说明符 - 假设为int。注意:C ++不支持default-int

错误C2040:'xscroll':'int'的间接级别与'float *'不同

错误C2440:'初始化':无法从'float **'转换为'int'

这是代码 Enemy.h

#include <windows.h>
#include "Player.h"

class Enemy
{
public:
Enemy(float xPos, float yPos);
Enemy(void);
~Enemy(void);

//update the position of the user controlled object.
void updatePosition(float timeFactor);

//loads all the enemy textures
void static loadTextures();

//creates a set number of enemies
void static createEnemies(int numEnemies, Enemy * enemyArray);

GLuint static enemyTex;
static float * xscroll;
static float * yscroll;
static Player * player;

private:
bool checkCollison(float x, float y, int radius);

float XPos;
float YPos;

};

尝试设置变量

Enemy::xscroll = &xscroll;
Enemy::yscroll = &yscroll;
Enemy::player = &player;

3 个答案:

答案 0 :(得分:1)

假设这些是定义,您需要包含类型(这是第一个错误):

float *Enemy::xscroll = ...;
Player *Enemy::player = ...;

对于第二个错误,xscroll似乎不是float,因此&xscroll不是float *,因此无法分配给Enemy::xscroll }。您需要确保变量的类型是正确的。

答案 1 :(得分:1)

我认为你把初始化和作业混为一谈。所有类静态变量必须从全局范围定义一次(即,定义在任何类或函数之外,但可以在命名空间中),并且可以在那时进行初始化。此定义与任何全局变量type identifier = initializer;的定义类似,但标识符包含范围运算符::

答案 2 :(得分:0)

也许编写setter / getter公共静态方法来改变变量是最好的方法吗?并将xscroll和其他人转为私有。

我认为这是一个更美丽的解决方案,而且代码会更简单。

相关问题