[Solved] Creating classes in cocos 2d-x

I am trying to create some basic classes and I’m having a lot of trouble even getting them to compile in xcode. Maybe I am missing something, can you help me review this code and see where I’m going wrong.

Power.h
#ifndef __Power__
#define __Power__

#include "cocos2d.h"

class Power : public cocos2d::DrawNode {
public:
    CREATE_FUNC(Power);
    virtual bool init();
    Power();
    ~Power();
};

#endif

Power.cpp
#include "Power.h"

bool init() {
    if(!DrawNode::init()) {
        return false;
    }
    return true;
}

Power::Power() {
    CCLOG("power const");
}

Power::~Power() {
    CCLOG("power descon");
}

This simple code fails to compile, I get build failures.

Undefined symbols for architecture x86_64:
  "Power::Power()", referenced from:
      Power::create() in HelloWorldScene.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)




 "Power::Power()", referenced from:
      Power::create() in HelloWorldScene.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Hi,

It seems to me that you forgot to add the new files to the target when adding them to xcode… it seems as if they are not compiled and thus not available for linkage.

Regards,

Arjen

Your init method is not defined in the cpp. You must prefix it with Power::

@ppl

I think you were right about this, it seems that if I use the default constructor as I was initially doing something gets messed up in the backend. I changed the constructor to something other than Power() and just passed in a test value Power(int val) and then the problem stopped.

Also using your method of prefixing my function also fixed the issue.