문제

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;
도움이 되었습니까?

해결책

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

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top