设置静态成员指针变量

发布于 2024-08-28 19:06:53 字数 963 浏览 5 评论 0原文

我正在尝试在类中设置静态指针变量,但我尝试设置的每个变量都会出现这些错误。

错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2024-09-04 19:06:53

我认为您将初始化与分配混淆了。所有类静态变量都必须在全局范围内定义一次(即定义在任何类或函数之外,但可以在命名空间中),并且可以在那时初始化。此定义看起来与任何全局变量的定义一样,类型标识符 = 初始值设定项;,只不过标识符包含作用域运算符 ::

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 ::.

你列表最软的妹 2024-09-04 19:06:53

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

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

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

Assuming those are the definitions, you need to include the type (that's the first error):

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

As for the second errors, it appears that xscroll is not a float, so &xscroll is not a float * and therefore cannot be assigned to Enemy::xscroll. You need to make sure the types of your variables are correct.

恍梦境° 2024-09-04 19:06:53

也许编写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文