设置静态成员指针变量
我正在尝试在类中设置静态指针变量,但我尝试设置的每个变量都会出现这些错误。
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
错误 C2040: 'xscroll' : 'int' 与 'float *' 的间接级别不同
错误 C2440: 'initializing' : 无法从 '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;
I'm trying to set a static pointer variable in a class but I'm getting these errors for each variable I try to set.
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2040: 'xscroll' : 'int' differs in levels of indirection from 'float *'
error C2440: 'initializing' : cannot convert from 'float **' to 'int'
Here is the code
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;
};
trying to set variables
Enemy::xscroll = &xscroll;
Enemy::yscroll = &yscroll;
Enemy::player = &player;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您将初始化与分配混淆了。所有类静态变量都必须在全局范围内定义一次(即定义在任何类或函数之外,但可以在命名空间中),并且可以在那时初始化。此定义看起来与任何全局变量的定义一样,
类型标识符 = 初始值设定项;
,只不过标识符包含作用域运算符::
。I think you're mixing up initialization with assignment. All class static variables have to be defined once, from global scope (i.e. the definition is outside any class or function, can be in a namespace however) and can be initialized at that time. This definition looks just like the definition of any global variable,
type identifier = initializer;
except that the identifier includes the scope operator::
.假设这些是定义,您需要包含类型(这是第一个错误):
至于第二个错误,似乎
xscroll
不是float
,因此 < code>&xscroll 不是float *
,因此不能分配给Enemy::xscroll
。您需要确保变量的类型正确。Assuming those are the definitions, you need to include the type (that's the first error):
As for the second errors, it appears that
xscroll
is not afloat
, so&xscroll
is not afloat *
and therefore cannot be assigned toEnemy::xscroll
. You need to make sure the types of your variables are correct.也许编写 setter/getter 公共静态方法来更改变量是最好的方法?并将 xscroll 和其他内容移至私有。
我认为这是更漂亮的解决方案,并且代码会更简单。
Maybe writing setter/getter public static methods to change variables is best way? And move xscroll and others sto private.
It's more beatiful solution, i think, and code is going to be more simple.