c++ - 无法解决cocos2dx中的 "error: candidate is:"

标签 c++ cocos2d-x cocos2d-x-3.0

当我尝试运行我的游戏时,我目前遇到以下错误的问题。我试图理解以下错误的含义,但我发现很难理解。我相信有不止一个错误,但我不知道去哪里找。我很想得到你的帮助!

[armeabi] Compile++ thumb: MyGame_shared <= BallSprite.cpp
[armeabi] Compile++ thumb: MyGame_shared <= Character.cpp
[armeabi] StaticLibrary  : libcocos2d.a
[armeabi] StaticLibrary  : libcocostudio.a
[armeabi] StaticLibrary  : libcocosbuilder.a
[armeabi] StaticLibrary  : libcocos3d.a
jni/../../../Classes/Character.cpp:26:5: error: prototype for 'int Character::getTurnCount()' does not match any in class 'Character'
 int Character::getTurnCount()
     ^
In file included from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:28:0,
                 from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../cocos2d.h:41,
                 from jni/../../../Classes/Character.h:4,
                 from jni/../../../Classes/Character.cpp:1:
/Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../platform/CCPlatformMacros.h:153:25: error: candidate is: virtual int Character::getTurnCount() const
 public: virtual varType get##funName(void) const;\
                         ^
jni/../../../Classes/Character.h:26:5: note: in expansion of macro 'CC_PROPERTY'
     CC_PROPERTY(int, _turnCount, TurnCount); 
     ^
jni/../../../Classes/BallSprite.cpp:62:27: error: prototype for 'BallSprite::PositionIndex BallSprite::getPositionIndex()' does not match any in class 'BallSprite'
 BallSprite::PositionIndex BallSprite::getPositionIndex()
                           ^
In file included from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:28:0,
                 from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../cocos2d.h:41,
                 from jni/../../../Classes/BallSprite.h:4,
                 from jni/../../../Classes/BallSprite.cpp:1:
/Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../platform/CCPlatformMacros.h:153:25: error: candidate is: virtual BallSprite::PositionIndex BallSprite::getPositionIndex() const
 public: virtual varType get##funName(void) const;\
                         ^
jni/../../../Classes/BallSprite.h:53:5: note: in expansion of macro 'CC_PROPERTY'
     CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex); 
     ^
make: *** [obj/local/armeabi/objs-debug/MyGame_shared/__/__/__/Classes/BallSprite.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [obj/local/armeabi/objs-debug/MyGame_shared/__/__/__/Classes/Character.o] Error 1
make: Leaving directory `/Users/michael/Desktop/CocoFolder/Puzzle/proj.android-studio/app'
Error running command, return code: 2.

字符代码

#include "Character.h"

USING_NS_CC;

Character::Character()
: _hp(0)
, _maxHp(0)
, _attack(0)
, _element(Element::None)
, _turnCount(0)
, _remainingTurn(0)
{
}

Character* Character::create()
{
    Character *pRet = new Character();
    pRet->autorelease();

    return pRet;
}

int Character::getTurnCount()
{
    return _turnCount;
}

void Character::setTurnCount(int turnCount)
{
    _turnCount = turnCount;
    _remainingTurn = _turnCount;
}


float Character::getHpPercentage()
{
    return _hp * 100.f / _maxHp;
}


bool Character::isAttackTurn()
{ 
    _remainingTurn--;

    if (_remainingTurn <= 0)
    { 
        _remainingTurn = _turnCount;
        return true;
    }

    return false;
}

int Character::getDamage(int ballCount, int chainCount, Character* attacker, Character* defender)
{
    float baseDamage = ballCount / 3.0 * 100;
    float chainBonus = powf(1.1, chainCount - 1);

    float elementBonus = getElementBonus(attacker->getElement(), defender->getElement());

    return baseDamage * chainBonus * elementBonus;
}

float Character::getElementBonus(Element attackElement, Element defenseElement)
{
    switch (attackElement)
    {
        case Element::Fire:
        {

            switch (defenseElement)
            {
                case Element::Wind:return 2;
                case Element::Water:return 0.5;
                default:return 1;
            }
            break;
        }
        case Element::Water:
        {
            switch (defenseElement)
            {
                case Element::Fire:return 2;
                case Element::Wind:return 0.5;
                default:return 1;
            }
            break;
        }
        case Element::Wind:
        {
            switch (defenseElement)
            {
                case Element::Water:return 2;
                case Element::Wind:return 0.5;
                default:return 1;
            }
            break;
        }
        case Element::Holy:
        {
            switch (defenseElement)
            {
                case Element::Shadow:return 2;
                default:return 1;
            }
            break;
        }
        case Element::Shadow:
        {
            switch (defenseElement)
            {
                case Element::Holy:return 2;
                default:return 1;
            }
            break;
        }
        default:
        {
            return 1;
        }
    }
}

字符标题

class Character : public cocos2d::Ref
{
public:

    enum class Element
    {
        Fire, 
        Water, 
        Wind, 
        Holy, 
        Shadow, 
        None, 
    };

protected:
    int _remainingTurn; 
    CC_SYNTHESIZE(int, _hp, Hp); 
    CC_SYNTHESIZE(int, _maxHp, MaxHp); 
    CC_SYNTHESIZE(int, _attack, Attack); 
    CC_SYNTHESIZE(Element, _element, Element); 
    CC_PROPERTY(int, _turnCount, TurnCount);

public:
    Character();
    static Character* create(); 

    float getHpPercentage(); 
    bool isAttackTurn(); 
    static int getDamage(int ballCount, int chainCount, Character* attacker, Character* defender); 

protected:
    static float getElementBonus(Element attackElement, Element defenseElement); 
};

#endif 

球 Sprite .cpp

#include "BallSprite.h"

USING_NS_CC;

BallSprite::BallSprite()
: _removedNo(0)
, _checkedX(false)
, _checkedY(false)
, _fallCount(0)
, _positionIndex(0, 0)
{
}

BallSprite* BallSprite::create(BallType type, bool visible)
{
    BallSprite *pRet = new BallSprite();
    if (pRet && pRet->init(type, visible))
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = nullptr;
        return nullptr;
    }
}

bool BallSprite::init(BallType type, bool visible)
{
    if (!Sprite::initWithFile(getBallImageFilePath(type)))
        return false;

    _ballType = type;

    setVisible(visible);

    return true;
}

void BallSprite::resetParams()
{
    _removedNo = 0;
    _checkedX = false;
    _checkedY = false;
    _fallCount = 0;
}

void BallSprite::resetPosition()
{
    setPosition(getPositionForPositionIndex(_positionIndex));
}

void BallSprite::getPositionIndex()
{ 
    return _positionIndex;
}

void BallSprite::setPositionIndex(PositionIndex positionIndex)
{
    _positionIndex = positionIndex;
    setTag(generateTag(_positionIndex));
}

void BallSprite::setPositionIndexAndChangePosition(PositionIndex positionIndex)
{
    setPositionIndex(positionIndex);
    resetPosition();
}

void BallSprite::removingAndFallingAnimation(int maxRemovedNo)
{
    removingAnimation(maxRemovedNo);  
    fallingAnimation(maxRemovedNo);
}

void BallSprite::removingAnimation(int maxRemovedNo)
{
    if (_removedNo > 0)
    {

        auto delay1 = DelayTime::create(ONE_ACTION_TIME * (_removedNo - 1));
        auto fade = FadeTo::create(ONE_ACTION_TIME, 0);
        auto delay2 = DelayTime::create(ONE_ACTION_TIME * (maxRemovedNo - _removedNo));
        auto removeSelf = RemoveSelf::create(false); 
        runAction(Sequence::create(delay1, fade, delay2, removeSelf, nullptr));
    }
}


void BallSprite::fallingAnimation(int maxRemovedNo)
{
    if (_fallCount > 0)
    {

        setPositionIndex(PositionIndex(_positionIndex.x, _positionIndex.y - _fallCount));

        auto delay = DelayTime::create(ONE_ACTION_TIME * maxRemovedNo);
        auto show = Show::create();
        auto move = MoveTo::create(ONE_ACTION_TIME, getPositionForPositionIndex(getPositionIndex()));
        runAction(Sequence::create(delay, show, move, nullptr));
    }
}

std::string BallSprite::getBallImageFilePath(BallType type)
{
    switch (type)
    {
        case BallType::Red: return "red.png";
        case BallType::Blue: return "blue.png";
        default: return "pink.png";
    }
}


Point BallSprite::getPositionForPositionIndex(PositionIndex positionIndex)
{
    return Point(BALL_SIZE * (positionIndex.x - 0.5) + 1,
                 BALL_SIZE * (positionIndex.y - 0.5) + 1);
}

int BallSprite::generateTag(PositionIndex positionIndex)
{
    return positionIndex.x * 10 + positionIndex.y;
}

BallSprite 标题

#include "cocos2d.h"

#define BALL_SIZE 106 
#define ONE_ACTION_TIME 0.2 

class BallSprite : public cocos2d::Sprite
{
public:

    enum class BallType
    {
        Blue, 
        Red, 
        Green, 
        Yellow, 
        Purple, 
        Pink, 
    };

    struct PositionIndex
    {
        PositionIndex()
        {
            x = 0;
            y = 0;
        }


        PositionIndex(int _x, int _y)
        {
            x = _x;
            y = _y;
        }

        int x; 
        int y; 
    };

    BallSprite();
    static BallSprite* create(BallType type, bool visible);
    virtual bool init(BallType type, bool visible); 

    CC_SYNTHESIZE(int, _removedNo, RemovedNo); 
    CC_SYNTHESIZE(bool, _checkedX, CheckedX); 
    CC_SYNTHESIZE(bool, _checkedY, CheckedY); 
    CC_SYNTHESIZE(int, _fallCount, FallCount); 
    CC_SYNTHESIZE_READONLY(BallType, _ballType, BallType); 
    CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex); 

    void setPositionIndexAndChangePosition(PositionIndex positionIndex); 
    void resetParams(); 
    void resetPosition(); 
    void removingAndFallingAnimation(int maxRemovedNo); 

    static std::string getBallImageFilePath(BallType type); 
    static cocos2d::Point getPositionForPositionIndex(PositionIndex positionIndex); 
    static int generateTag(PositionIndex positionIndex); 

protected:
    void removingAnimation(int maxRemovedNo); 
    void fallingAnimation(int maxRemovedNo); 
};

#endif

最佳答案

CC_PROPERTY(int, _turnCount, TurnCount);

虚方法定义应该是这样的,因为在声明中它是常量。

int Character::getTurnCount() const
{
    return _turnCount;
}

而且getter方法的返回类型有问题,应该是这样的。

CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex);

PositionIndex BallSprite::getPositionIndex() const
{
    return _positionIndex;
}

编辑

不能直接访问PositionIndex,与BallSprite一起使用

BallSprite::PositionIndex BallSprite::getPositionIndex() const
{
    return _positionIndex;
}

关于c++ - 无法解决cocos2dx中的 "error: candidate is:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43776458/

相关文章:

ios - 浅色 Cocos2dx 上的 GL 混合

C++ Sleep 函数没有按预期工作

c++ - 跨 Windows DLL 模块边界清理堆分配资源的问题

linux - 适用于 Linux 的 Cocos2d-x 编辑器?

crash - cocos2dx 2.1.2在设备上启动时android崩溃

c++ - 如何在cocos2d-x中制作一个可以包含在HelloWorld.cpp中的Layer文件...?

c++ - 带有 cocos2d-x 3 beta 2 的 Xcode 5.1 中的链接器错误

c++ - 像列表一样订购但按键访问?

c++ - 从键盘接受树节点以确定它的高度

c++ - 从 C++ 中的二维 vector 中的特定索引获取值