繁体   English   中英

在非静态成员函数错误之外无效使用'this'?

[英]invalid use of 'this' outside of a non-static member function error?

我正在使用CCTouchTargetedDelegate和CCSprite子类。 在定义委托方法时,我无法在函数内部使用“this”。

正如先前提到的问题所回答的那样,我无法使用具有使用范围分辨率的函数的类的名称,因为它然后给了我“'ccTouchBegan'的外线定义与'mygames :: DragSprite中的任何声明都不匹配的错误“”

我也尝试在.h文件中声明该函数,但似乎没有任何效果。

我的代码如下: -

.h文件

#pragma once
#include "cocos2d.h"




namespace mygames
{

    class DragSprite: public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate
    {
        public:
            DragSprite* createWithFile(const char *pszFileName);
            bool isTouchingOnSprite(cocos2d::CCPoint  touch);
            virtual bool init();
        bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
        static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2);
        private:
            bool isDrag;
            cocos2d::CCPoint whereTouch;
    };
}

.cpp文件

#include "DragSprite.h"


using namespace mygames;


bool DragSprite::init()
{
    if (!CCSprite::init()) {
        return false;
    }

    whereTouch = cocos2d::CCPointZero;
    isDrag = false;

    return true;
}
DragSprite* DragSprite::createWithFile(const char *pszFileName)
{
    DragSprite *pSprite = new DragSprite();
    if (pSprite&&pSprite->initWithFile(pszFileName))
    {
        cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(pSprite, 0, true);
        pSprite->autorelease();
        return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
}

bool DragSprite::isTouchingOnSprite(cocos2d::CCPoint  touch)
{

    if (this->boundingBox().containsPoint(touch)) {
        return true;
    }else
    {
        return false;
    }    

}
static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2)
{
    return ccp(v1.x-v2.x, v1.y-v2.y);

}
//CCTargetedTouchDelegate
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

    cocos2d::CCPoint touchPoint = pTouch->getLocation();

    if (this->isTouchingOnSprite(touchPoint)) {
        this->whereTouch = ccpSub(this->position, touchPoint);
        return true;
    }
    return false;
}

错误屏幕截图: -

我在这里失踪了什么?

只是出于好奇心

AS在答案中建议,如果我使用

bool DragSprite::ccTouchBegan

那么,它还会调用delegete函数吗? 或者只是我的DragSprite类中的函数。 我的意思是,该功能是否仍会被覆盖? 嗯...它是在CCTargetedTouchDelegete中声明的方法。 我猜它是一个抽象的函数。

bool ccTouchBegan(

需要是

bool DragSprite::ccTouchBegan(

你不应该首先需要this

为什么不将您的功能定义为

bool DragSprite::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

这是你在.h文件中的DragSprite类中定义它的方法。

这个

bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

定义了一个独立的功能。 这只能在类成员函数中使用。 要使其成为已定义的类成员,您需要限定名称:

bool DragSprite::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

- = - = - = - =

我看到每个人都立刻跳了进来。 目标C的语法不同。 它用

@implementation DragSprite
 . . . . 

@end 

指定类并要求 - 表示非静态成员函数。

另一个区别是Objective C需要自引用来调用成员函数

[self DragSprite] ;

C ++没有

DragSprite () ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM