i'm trying to create a callback function in Cocos2d-X. I have a singleton class (AdsMgr). In the AdsMgr, i want to store the function pointer. How should i replace the callBackPauseResume with the passing in parameter?
AdsMgr.h
class AdsMgr
{
private:
static bool isAdsEnabled();
public:
static void(*callBackPauseResume)(int index);
static void initAds(void(*incallback)(int index));
}
AdsMgr.mm
void AdsMgr::initAds(void(*incallback)(int index))
{
callBackPauseResume = incallback;
// incallback(1);
}
MainScene.cpp
if(btn4 && btn4->getBoundingBox().containsPoint(location))
{
CCLOG("SHOW INTERSTITIAL");
AdsMgr::initAds(MainScene::pauseResumeDuringAds);
}
void MainScene::pauseResumeDuringAds(int inFlag)
{
switch (inFlag) {
case 0:
// Pause game
break;
case 1:
// Resume Game
break;
default:
break;
}
}
I'm unable to compile and encountered the following errors:
Undefined symbols for architecture arm64:
"AdsMgr::callBackPauseResume", referenced from:
AdsMgr::initAds(void (*)(int)) in AdsMgr.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Judging by your compile error, you haven't defined your callBackPauseResume
variable. That's what the error is saying.
In addition to its declaration inside the class, you also need to add a definition for it, outside the class, in AdsMgr.mm
:
void (*AdsMgr::callBackPauseResume)(int) = nullptr;