0

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

错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数

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

错误 C2440:“正在初始化”:无法从“浮点 **”转换为“整数”

这是代码 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;
4

3 回答 3

1

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

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

至于第二个错误,似乎xscroll不是 a float,所以&xscroll不是 a float *,因此不能分配给Enemy::xscroll。您需要确保变量的类型是正确的。

于 2010-04-12T23:52:11.603 回答
1

我认为您将初始化与分配混为一谈。所有类静态变量都必须在全局范围内定义一次(即定义在任何类或函数之外,但是可以在命名空间中)并且可以在那时初始化。这个定义看起来就像任何全局变量的定义,type identifier = initializer;除了标识符包括范围运算符::

于 2010-04-12T23:53:00.557 回答
0

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

我认为这是更漂亮的解决方案,并且代码会更简单。

于 2010-04-13T00:52:30.223 回答