如何避免在C ++的{h}文件中声明的对象的多重定义

时间:2015-10-29 17:11:28

标签: c++ c++11

我有一个GameUI.hRound1.cppGameUI.cpp文件。

在GameUI.h中,我声明了:

static cocos2d::sprite* battery;

Round1.cppGameUI.cpp都包含GameUI.h,这会产生多重定义问题并提供

  

链接器命令失败,退出代码为1 - 错误

GameUI.h文件是

#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文件是

#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文件

#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();
}

0 个答案:

没有答案