0

I want to define a custom non-static method like this:

here is the .h file

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Scene
{

private:
    Director *dir;
    SpriteFrameCache *spriteCache;
    Size visiableSize;
    Vec2 visialbeOrigin;
    cocos2d::Sprite getMySpriteByName(const std::string &name);
    cocos2d::Sprite getBrickByName(const std::string &name);

public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

And here is how getMySpriteByName() and getBrickByName defined in .cpp file:

cocos2d::Sprite HelloWorld::getMySpriteByName(const std::string &name) {
    return Sprite::createWithSpriteFrameName(name);

}

cocos2d::Sprite HelloWorld::getBrickByName(const std::string &name) {
    auto brickSprite = this->getMySpriteByName(name);
    return brickSprite;
}

But the IDE or the compiler won't let me do that

What have I done wrong?

And the build error is:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0415   no suitable constructor exists to convert from "cocos2d::Sprite *" to "cocos2d::Sprite" onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    14  
Error (active)  E0330   "cocos2d::Sprite::Sprite(const cocos2d::Sprite &)" (declared at line 725 of "d:\cocos\onestep\cocos2d\cocos\2d\CCSprite.h") is inaccessible onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    19  
Error (active)  E0330   "cocos2d::Sprite::Sprite(const cocos2d::Sprite &)" (declared at line 725 of "d:\cocos\onestep\cocos2d\cocos\2d\CCSprite.h") is inaccessible onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    20  
Error (active)  E0020   identifier "visibleSize" is undefined   onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    51  
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • Totally unrelated to your problem or question, but don't use symbols with double leading underscore in your program (like for example `__HELLOWORLD_SCENE_H__`). Such are reserved in all scopes. See e.g. [this question and answers for more information](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Aug 09 '17 at 06:00
  • As for your problem, if you post a question about build errors you should know by know that we need to see the errors. – Some programmer dude Aug 09 '17 at 06:01
  • @Someprogrammerdude that code was generated by cocos2d framework, not familiar with c++ yet, maybe you should issue this [here](https://github.com/cocos2d/cocos2d-x) :D – armnotstrong Aug 09 '17 at 06:02

1 Answers1

2

you missed the * (pointer)

in .h should be

cocos2d::Sprite* getMySpriteByName(const std::string &name);
cocos2d::Sprite* getBrickByName(const std::string &name);

in .cpp should be

cocos2d::Sprite* HelloWorld::getMySpriteByName(const std::string &name) {
    return Sprite::createWithSpriteFrameName(name);

}

cocos2d::Sprite* HelloWorld::getBrickByName(const std::string &name) {
    auto brickSprite = this->getMySpriteByName(name);
    return brickSprite;
}
raksa
  • 898
  • 6
  • 17