我想设置一个静态指针变量的一类,但我发现这些错误的每个变量我尝试设置。

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

错误C2040: 'xscroll':在从间接水平 'INT' 不同 '浮动*'

错误C2440:初始化:不能从 '浮动**' 转换为 '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;
有帮助吗?

解决方案

我觉得你初始化混合了任务。所有类静态变量必须定义一次,从全局范围(即,定义为外任何类别或功能,可以在命名空间不过),并且可以在那个时候被初始化。这个定义看起来就像任何全局变量的定义中,type identifier = initializer;除了所述标识符包括范围操作符::

其他提示

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

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

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

也许写的setter /吸气公共静态方法来改变变量是最好的办法吗?而此举xscroll和其他STO私人。

它更beatiful溶液,我想和代码将是更简单的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top