0

I have a GameUI.h, Round1.cpp and GameUI.cpp file.

In GameUI.h, I have declared:

static cocos2d::sprite* battery;

Both Round1.cpp and GameUI.cpp has included GameUI.h and this is creating multiple definition problem and giving

linker command failed with exit code 1 - Error

GameUI.h file is

#ifndef __GAME_UI_H__
#define __GAME_UI_H__

#include "cocos2d.h"
#include "common.h"

class GameUI : public cocos2d::Layer
{
public:

    static cocos2d::Sprite *_batteryPannel;
};

#endif

GameUI.cpp file is

#include <GameUI.h>

USING_NS_CC;

Node* GameUI::createBatteryPanel()
{
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto batteryNode = Node::create();    
    _batteryPannel = CommonFunc::createSpriteFromCache("BatteryPanel.png", 0, 0);
    batteryNode->addChild(_batteryPannel, 2);

return batteryNode;
}

Round1.cpp file I have

#include "Round1.h"
#include "GameUI.h"

USING_NS_CC;

// on "init" you need to initialize your instance
bool RoundA::init()
{
  auto batteryPosition = GameUI::_batteryPanel->getPosition();
}
Gunjan
  • 86
  • 1
  • 10
  • @n.m. I feel this should be closed as a duplicate of [this](http://stackoverflow.com/questions/185844/initializing-private-static-members) instead of the current target. – NathanOliver Oct 29 '15 at 17:26
  • @NathanOliver I'm not so sure, what will OP say? – n. m. could be an AI Oct 29 '15 at 17:43
  • @n.m I think NathanOliver link has answer to my question. Although your link was also very useful and help me understand things better. – Gunjan Nov 05 '15 at 09:59
  • Possible duplicate: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – n. m. could be an AI Nov 05 '15 at 10:10
  • Reopened, the old target isleft as a comment. – n. m. could be an AI Nov 05 '15 at 10:10
  • Can you create a minimal, complete, verifiable exampl (http://stackoverflow.com/help/mcve)? As far as I can see this shouldn't result in link error since `battery` is TU private. The thing that should happen is that when `Round1.cpp` refers to `battery` it would not be the same pointer as if `GameUI.cpp` refers to `battery'. – skyking Nov 05 '15 at 10:19
  • @skyking no, static members are not TU private. – n. m. could be an AI Nov 05 '15 at 10:23
  • @n.m. As written I see no evidence that it **is** a static member, in any way. Even if we assume it was written that way inside a class definition (there's missing context here) it would not be a definition of the pointer. – skyking Nov 05 '15 at 10:28
  • @skyking hmm you are right, bad reading comprehension on my part. – n. m. could be an AI Nov 05 '15 at 10:32
  • The code you have posted would not cause a multiple definition error unless it is referenced from an inline function . An MCVE would help a lot. – M.M Nov 05 '15 at 11:52
  • Basically, error was coming as I have not define it in the GameUI.cpp file. Once I added cocos2d::sprite* battery = NULL it worked fine. So what i understand from n.m link is it was declared but not defined, which is what is directly mentioned in @NathanOliver link. – Gunjan Nov 05 '15 at 13:31
  • @Gunjan "Not defined" is the opposite of "multiple definition problem", which is why everyone is confused. – n. m. could be an AI Nov 05 '15 at 16:25
  • It was not defined in .cpp file, so whenever I am trying to use it, it might be creating a new definition. Thats what I understand, but my problem got resolved by putting the definition on the top of the GameUI.cpp. Also, the error is the one which I posted. – Gunjan Nov 06 '15 at 15:45

0 Answers0