Daniel Sega Daniel Sega - 1 month ago 5
C++ Question

Header file is inaccessible

I'm using cocos2d-x to create my project but I'm getting this error:


Error (active) "CocosDenshion::SimpleAudioEngine::SimpleAudioEngine()"
(declared at line 256 of "c:\MyGame
\cocos2d\cocos\audio\include\SimpleAudioEngine.h") is
inaccessible MyGame c:\MyGame \Classes\MyGame .cpp


So I am including the SimpleAudioEngine.h file into my CPP file, to work with it. As you can see from the error, to use the SimpleAudioEngine, I need to use the CocosDenshion namespace first, but as soon as I am done typing:

CocosDenshion::SimpleAudioEngine()


Visual Studio shows this error to me, VS can show me the declaration, so that tells me that it knows where the header is and it can be read. So I don't know what is the issue for being inaccessible. What are some reason for header files to be inaccessible?


MyGame.cpp


#include "MyGame.h"
#include "SimpleAudioEngine.h"
#include "GlobalVariables.h"

USING_NS_CC;

Scene* MyGame::createScene()
{
auto scene = Scene::create();
auto layer = MyGame::create();
scene->addChild(layer);
return scene;
}

bool MyGame::init()
{
if (!Layer::init())
{
return false;
}
is_dragged = false;

const char* MUSIC_PATH = "Music/Main_Theme_loop.ogg";
initTouch();
initTiled();
tempSetupSprite();

debugDrawLine();

this->scheduleUpdate();
return true;
}



MyGame.h


#include "GameSprite.h"
#include "GameMap.h"

class MyGame : public cocos2d::Layer
{
private:
void update(float dt);
void initTouch();
void initTiled();

void tempSetupSprite();
void debugDrawLine();
public:
static cocos2d::Scene* createScene();

virtual bool init();

virtual bool onTouchBegan(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchEnded(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchMoved(cocos2d::Touch* _touch, cocos2d::Event* _event);
virtual void onTouchCancelled(cocos2d::Touch* _touch, cocos2d::Event* _event);

CREATE_FUNC(MyGame);
private:
bool is_dragged;

Vec2 first_touch;
Vec2 last_drag_touch;

GameSprite* sprite;
GameMap* map;
};

Answer

Looking at the documentation of cocos2d-x (http://www.cocos2d-x.org/reference/native-cpp/V3.5/de/d8f/class_cocos_denshion_1_1_simple_audio_engine.html). The constructor is protected. You have to use the following method to get the shared static simple audio engine instance:

getInstance()