{Solved} Invalid use of non-static data member

Hi, Please help me I’m from java background.

I’m working on android project in eclips Luna and cocos2dx 3.3 version.

I declare sprite Obstacle1 and a method
void ResetObstacle(Node * node); in heder file GameScreen.h.

Then in GameScreen.cpp class when I use the sprite Obstacle1 in method ResetObstacle then I’m getting the following error :-

“invalid use of non-static data member ‘GameScreen::Obstacle1’”

My GameScreen.h file is as follows :-

#ifndef GAME_SCENE_H
#define GAME_SCENE_H
#include “InfiniteParallaxNode.h”
#include “cocos2d.h”
USING_NS_CC;
class GameScreen : public Layer
{
public:
// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
//............menu.................

void GoToPauseScene(Ref *pSender);
void GoToGameOverScene(Ref *pSender);
void ResetObstacle(Node * node);
void update(float) override;

// implement the "static create()" method manually
CREATE_FUNC(GameScreen);
cocos2d::Sprite* bgtop;
cocos2d::Sprite* bgUp;
cocos2d::Sprite* bgBottom;
InfiniteParallaxNode* _backgroundElements;
cocos2d::Sprite* playerSprite;
cocos2d::Sprite *Obstacle1;

};

#endif // GAME_SCENE_H

And GameScreen.cpp is as follows:-

#include “GameScene.h”
#include “GamePausedScene.h”
#include “GameOverScene.h”
#include “InfiniteParallaxNode.h”
USING_NS_CC;
Label label1;
Scene
GameScreen::createScene() {
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = GameScreen::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

// on “init” you need to initialize your instance
bool GameScreen::init() {
_backgroundElements = InfiniteParallaxNode::create();
//////////////////////////////
// 1. super init first
if (!Layer::init()) {
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

GameScreen::bgUp = Sprite::create("gameBackground top.png");
//bgUp->setPosition(Vec2(origin.x +bgUp->getContentSize().width, origin.y+00 +bgUp->getContentSize().height));
this->addChild(bgUp, 0);

bgUp->setAnchorPoint(Point::ZERO);

GameScreen::Obstacle1 = Sprite::create(“obj1.png”);
this->addChild(Obstacle1, 2);
//Obstacle1->setPosition(500,200);

Obstacle1->setPosition(Vec2(480, 550));
auto moveToObstacle1 = MoveTo::create(3, Vec2(50, 60));
auto delay = DelayTime::create(50.25f);
auto seq3 = Sequence::create(moveToObstacle1,
		CallFuncN::create(CC_CALLBACK_1(GameScreen::ResetObstacle, this)),
		delay, NULL);

Obstacle1->runAction(RepeatForever::create(seq3));

scheduleUpdate();

return true;

} //

void ResetObstacle(Node * node) {
int pos = cocos2d::random(200, 400);
GameScreen::Obstacle1->setPosition(Vec2(pos, 550));

}

Please help me I’m from java background.

Thanks.

As the obstacle1 is not the static data member,so need not to use “GameScreen::” before that.
Use it as below,it will work.
Obstacle1->setPosition(Vec2(pos, 550));
in place of
GameScreen::Obstacle1->setPosition(Vec2(pos, 550));

and do it same for bgUp.

Yess,1 more mistake is there.Use “GameScreen::” while defining any function of class “GameScreen.h”
means replace this line- void ResetObstacle(Node * node)
by
void GameScreen::ResetObstacle(Node * node)

Hi ritusin,

Thanks for your quick reply.

I made changes as you said but same error is coming.

But I’m not getting any error in bgUp.

Use “GameScreen::” while defining any function of class “GameScreen.h”
means replace this line- void ResetObstacle(Node * node)
by
void GameScreen::ResetObstacle(Node * node)

The overall change you required to do in your above code is:

void GameScreen::ResetObstacle(Node * node) {
int pos = cocos2d::random(200, 400);
GameScreen::Obstacle1->setPosition(Vec2(pos, 550));

}

“GameScreen::” is need not to add before Obstacle1,It can work as such “Obstacle1”.“GameScreen::” is required to add when you define the variable as static.

Hi ritusin,

Thanks for ypur reply.

prototype for ‘void GameScreen::ResetObstacle(cocos2d::Ref*)’ does not match any in class ‘GameScreen’

error is coming.

in .h file,what you have written:
void ResetObstacle(Node * node); or void ResetObstacle(Ref *pSender)

according to the function declare in “.h” file,you have to define the same in “.cpp” as:
void GameScreen::ResetObstacle(Node * node) or
void GameScreen::ResetObstacle(Ref *pSender)

hi ritusin,

I checked both of them with no success.

presently I’m using
void ResetObstacle(Node * node);

in .h and .cpp both.

Thanks for reply.

Hi, ritusin ,

I restart eclipse and clean and build the project solved the problem.

Thank u for helping me regarding this issue.

Great!
This was the only issue in your previous code:
“void GameScreen::ResetObstacle(Node * node)”

Sometimes eclipse does not read the changes instantly and clean build is required.:slight_smile: