Question

I'm following a Cocos2D-X tutorial for iOS on XCode 5 and the instructor started off by creating a bunch of classes and header files. Among those is a header file called "Utils.h" that contains some static functions

class Utils
{
public:
    static Game* gameLayer();
    static HUD* hudLayer();
    static cocos2d::CCLayer* layerWithTag(int tag);
    static cocos2d::CCSize s();
    static cocos2d::CCAnimate* getAnimationWithFrames(int from, int to);
    static void scaleSprite(cocos2d::CCSprite * sprite);
    static float getScale();
    static void setScale(float s);
    static float getArtScaleFactor();
    static void setArtScaleFactor(int s);
};

The s() functions is defined in Utils.cpp to just return CCDirector::sharedDirector()->getWinSize();

Now, the problem is: when the s() function is called in my main menu scene (Utils::s().width for instance), I get this error "Linker command failed with exit code 1 (use -v to see invocation)" and "Symbol(s) not found for architecture i386". However, when I deleted the s() function definition from Utils.cpp and just added the function in the class in the header file so that it becomes

//Utils.h
class Utils
{
public:
     .......
     static cocos2d::CCSize s()
     {
         return CCDirector::sharedDirector()->getWinSize();
     }
     ......

It compiles without an error...

Why is it that I get this error when the function is in the cpp file instead of the header file? I know my problem is already solved but it's just driving me crazy

Thanks! :)

Edit: I thought this was a given but to be clearer, in the main menu scene I included Utils.h not Utils.cpp

Was it helpful?

Solution

Please note that such errors also appear when you have declared a function in one file (say Cpp) and not in the .h file. Most common reason that i have seen is because one declares a function in the .h file but forget to give the implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top