-
MoseHasLikes 0Problem Description
Hi there.
I was trying to create a class to store global variables such as booleans, integers so that I could read the data in different cpps.
However, I failed.
Could you please check it if there is anything wrong? Thanks.
(Basically I just copy and paste another class A. Class A did run successfully but my Global did not.)
class Global { public: Global(){ _instance = 0; globalB = false; } static Global* getInstance(){ if (!_instance) _instance = new Global(); return _instance; } bool getGlobalB(){ return globalB; } void setGlobalB(const bool b){ globalB = b; } private: static Global* _instance; bool globalB; };
Thank you.
-
MoseHasLikes 0
Is that because I did not use friend to bind the class?
But I still get this error message.
1>AppDelegate.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification 1> Creating library D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.lib and object D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.exp 1>GameLayer.obj : error LNK2001: unresolved external symbol "private: static class Global * Global::_instance" (?_instance@Global@@0PAV1@A) 1>D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.exe : fatal error LNK1120: 1 unresolved externals
-
Sonar Systems adminLikes 0
Try removing static from _instance and see if that works
This reply has been verified.
-
MoseHasLikes 0
Error: a nonstatic member reference must be relative to a specific object.
-
Sonar Systems adminLikes 0
Try
int _instance;
-
MoseHasLikes 0
Sorry I left my code in the office. I do not have the platform at home. I will test it tomorrow and report it.
-
Sonar Systems adminLikes 0
ok
This reply has been verified.
-
MoseHasLikes 0
Without static declaration, it will go wrong with the same err message.
I have modified my code as follows.
class GlobalTest { public: static GlobalTest* getInstance() { if (!_instance) { _instance = new int(); log("new Global"); } //return _instance; return nullptr; } bool getGlobalB(){ return globalB; } void setGlobalB(const bool b){ globalB = b; } private: GlobalTest(){ _instance = 0; globalB = false; } static int* _instance; bool globalB; };
And still it went wrong.
1>AppDelegate.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification 1> Creating library D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.lib and object D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.exp 1>GameLayer.obj : error LNK2001: unresolved external symbol "private: static int * GlobalTest::_instance" (?_instance@GlobalTest@@0PAHA) 1>D:\home\MoseHas\popstartest\proj.win32\Debug.win32\popstartest.exe : fatal error LNK1120: 1 unresolved externals
-
Sonar Systems adminLikes 0
What are you actually trying to achieve?
This reply has been verified.
-
MoseHasLikes 0
I want to create a class which stores the global boolean variables so that I can call it anywhere anytime.
-
Sonar Systems adminLikes 0
Why do you need a class?
-
MoseHasLikes 0
I just want to manage all the bools in a class.
Besides, he wrote his own getInstance() for use. I want to learn it as well so that I can create a class my own.
-
Sonar Systems adminLikes 0
Who’s he?
-
MoseHasLikes 0
This is an open source code for popstar tutorial. The auther wrote some his own classes.
-
Sonar Systems adminLikes 0
Message him to see if he had any of these problems.
-
MoseHasLikes 0
I have solved this problem. I found someone else’s global setting method.
/* * Global.h */ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ #include "cocos2d.h" USING_NS_CC; class Global { public: static bool resumable; static bool isHammered; static bool isDoubled; static bool isColored; static bool mainMenuBool; }; #endif //_GLOBAL_H_
/* * Global.cpp */ #include "Global.h" bool Global::resumable = false; bool Global::isHammered = false; bool Global::isDoubled = false; bool Global::isColored = false; bool Global::mainMenuBool = true;
The declaration here I think is really weird. However, it worked successfully.
-
MoseHasLikes 0
Although I solved the problem, I failed to build apk now. The error message looked like the global variables were not allowed. I am not sure.
D:\home\MoseHas\popstartest>cocos compile -p android -ap22 Running command: compile Building mode: debug Android platform not specified, searching a default one... running: '"D:\cocos_workspace\Android\sdk\tools\android" update project -t andro id-22 -p D:\home\MoseHas\popstartest\proj.android' Updated project.properties Updated local.properties Updated file D:\home\MoseHas\popstartest\proj.android\proguard-project.txt It seems that there are sub-projects. If you want to update them please use the --subprojects parameter. Android platform not specified, searching a default one... running: '"D:\cocos_workspace\Android\sdk\tools\android" update lib-project -p D :\home\MoseHas\popstartest\proj.android\../cocos2d/cocos/platform/android/java - t android-22' Updated project.properties Updated local.properties Updated file D:\home\MoseHas\popstartest\proj.android\..\cocos2d\cocos\platform\ android\java\proguard-project.txt building native NDK build mode: debug NDK_TOOLCHAIN_VERSION: 4.9 running: '"D:\cocos_workspace\android-ndk-r10e\ndk-build" -C D:\home\MoseHas\pop startest\proj.android -j8 NDK_MODULE_PATH=D:\home\MoseHas\popstartest\proj.andro id\../cocos2d;D:\home\MoseHas\popstartest\proj.android\../cocos2d/cocos;D:\home\ MoseHas\popstartest\proj.android\../cocos2d/external NDK_TOOLCHAIN_VERSION=4.9 N DK_DEBUG=1' make.exe: Entering directory `D:/home/MoseHas/popstartest/proj.android' [armeabi] Compile++ thumb: cocos2dcpp_shared <= main.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= AppDelegate.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= ComboEffect.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= FloatWord.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= GameData.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= GameLayer.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= GameScene.cpp [armeabi] Compile++ thumb: cocos2dcpp_shared <= Global.cpp In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/GameScene.h:4, from jni/../../Classes/GameScene.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ compilation terminated. In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/GameData.cpp:2: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/FloatWord.h:4, from jni/../../Classes/FloatWord.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/GameLayer.h:4, from jni/../../Classes/GameLayer.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ cIn file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos /3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/AppDelegate.h:4, from jni/hellocpp/main.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/Global.h:4, from jni/../../Classes/Global.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ ccomIn file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/co cos/3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/ComboEffect.h:4, from jni/../../Classes/ComboEffect.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ In file included from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../base/ZipUtils.h:36:0, from D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/ 3d/../cocos2d.h:56, from jni/../../Classes/AppDelegate.h:4, from jni/../../Classes/AppDelegate.cpp:1: D:/home/MoseHas/popstartest/proj.android/../cocos2d/cocos/3d/../platform/android /CCFileUtils-android.h:37:35: fatal error: android/asset_manager.h: No such file or directory #include "android/asset_manager.h" ^ cocaomkcocmoemp.omopmepixmpmipeil:pipli lailialat*lalta*ti*atait io[titoioonbioi nojn /ono nl ton nt ctea t etler/tetrearmreremrmmiermrimainbmimniina/inianoatbna ntajtesataet-eddtetdeed.bede.du. gd.d ./ c. . oc os2dcpp_shared/__/__/Classes/GameScene.o] Error 1 make.exe: *** Waiting for unfinished jobs.... make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/hellocpp/main.o] E rror 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/AppD elegate.o] Error 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/Comb oEffect.o] Error 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/Floa tWord.o] Error 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/Game Data.o] Error 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/Game Layer.o] Error 1 make.exe: *** [obj/local/armeabi/objs-debug/cocos2dcpp_shared/__/__/Classes/Glob al.o] Error 1 make.exe: Leaving directory `D:/home/MoseHas/popstartest/proj.android' Ndk build failed!
-
Sonar Systems adminLikes 0
Try removing the global variable to see if it runs.
Login to reply