【问题标题】:invalid use of 'this' outside of a non-static member function error?在非静态成员函数错误之外无效使用“this”?
【发布时间】:2014-06-21 09:03:36
【问题描述】:

我将 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;
}

错误屏幕截图:-

我在这里错过了什么?

只是出于好奇

如答案中所建议,如果我使用

bool DragSprite::ccTouchBegan

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

【问题讨论】:

  • 错误信息还不够清楚吗?你需要DragSprite::
  • 您在 bool ccTouchBegan 定义中缺少类范围。
  • @BryanChen:好的,我明白了,但是我应该如何使用类指针。实际上我正在从 Objective-C 移植代码。并且已经使用了“自我”?
  • @DemonSOCKET 在 C++ 中有几个地方需要显式的 this,在大多数其他地方(包括这里),它被认为是不好的风格。

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


【解决方案1】:

为什么不将你的函数定义为

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

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

【讨论】:

  • 它是在 CCTargetedTouchDelegete 中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做呢?它还会调用deletete函数吗?或者只是我的 DragSprite 类中的函数。我的意思是,这个函数还会被覆盖吗?
  • 我会回答这个问题,但是由于您还专门询问了另外两个回答您问题的人,所以我会先给他们一个机会。 :-)
  • 嗯......我没有得到任何令人满意的答案。那你能帮我一下吗?
【解决方案2】:
bool ccTouchBegan(

需要

bool DragSprite::ccTouchBegan(

首先你不应该需要this

【讨论】:

  • 它是在 CCTargetedTouchDelegete 中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做呢?它还会调用deletete函数吗?或者只是我的 DragSprite 类中的函数。我的意思是,这个函数还会被覆盖吗?
  • @DemonSOCKET 嗯,好问题,我认为您的移植缺少另一个元素。在 C++ 中,虚函数是可选的,与 obj-c 不同。您想使用virtual 使此方法成为“虚拟”函数,然后它将像在 obj-c 中一样工作。 en.wikipedia.org/wiki/Virtual_function 了解更多信息。只需在函数定义前添加virtual
  • OK.. 好吧,因为它已经在我继承的 CCTargetedTouchDelegate 类中声明,就像下面的 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);返回假; };我还需要在 .h 文件中声明吗?如果是,那么带或不带虚拟关键字?
  • @DemonSOCKET virtual 需要在类定义中,在.h 文件中
【解决方案3】:

这个

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

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

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

-=-=-=-=

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

@implementation DragSprite
 . . . . 

@end 

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

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

[self DragSprite] ;

C++ 没有

DragSprite () ;

【讨论】:

  • 它是在 CCTargetedTouchDelegete 中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做呢?它还会调用deletete函数吗?或者只是我的 DragSprite 类中的函数。我的意思是,这个函数还会被覆盖吗?
  • 下一个问题是你要移植什么?调用委托的代码是否也在 C++ 中?如果是目标 C,那将是一个问题。
  • 好吧.. 不,它不在目标 C 中。通过移植我的意思是我只是在这里使用逻辑。因为 cocos2d-x 和 cocos2d-iPhone 基本相同。抱歉,如果我给您造成任何困惑。
  • 也.. 我对 cocos2dx 和 c++ 非常陌生。所以只是为了提示,是否必须在 .h 文件中声明任何基类的虚函数(非纯)?
  • 是的。这是目标 C 和 C++ 之间的另一个区别。它更大。所有成员函数都必须在头文件中声明。 Objective C 允许在没有声明的情况下在主体中定义成员。
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 2015-06-26
  • 1970-01-01
相关资源
最近更新 更多