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

9
我正在使用由CCSprite子类定义的类,并且结合CCTouchTargetedDelegate来使用。在定义委托方法时,我无法在函数内部使用"this"。
正如之前提出的问题所回答的那样,我不能使用带有作用域分辨符的函数名称,因为这会导致错误:“ccTouchBegan的非行内定义与'mygames :: DragSprite'中的任何声明不匹配”。
我还尝试在.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 File

#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

那么,它还会调用委托函数吗?还是只调用 DragSprite 类中的函数?我的意思是,函数是否仍然被覆盖? 嗯...... 这是在 CCTargetedTouchDelegate 中声明的方法。我想它是一个抽象函数。

4
错误信息不够清晰吗?你需要使用DragSprite:: - Bryan Chen
1
你在 bool ccTouchBegan 的定义中漏掉了类作用域。 - juanchopanza
@BryanChen:好的,我明白了,但是我该如何使用类指针呢?实际上,我正在从Objective-C移植代码。那里有使用“self”的地方? - Pawan Joshi
@DemonSOCKET 在C++中有一些需要显式使用this的地方,在大多数其他地方(包括这里)使用它被认为是不好的风格。 - Karthik T
3个回答

22
bool ccTouchBegan(

需要成为

bool DragSprite::ccTouchBegan(

你一开始就不应该需要this


这是在CCTargetedTouchDelegate中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做,它仍然会调用委托函数吗?还是只调用来自我的DragSprite类的函数?我的意思是,该函数是否仍然被覆盖? - Pawan Joshi
1
@DemonSOCKET 嗯,好问题。我认为你的移植还缺少一个元素。在C++中,虚函数是选择性的,不像Objective-C。你需要使用“virtual”将此方法作为“虚拟”函数,然后它将像Objective-C一样工作。请参阅http://en.wikipedia.org/wiki/Virtual_function以获取更多信息。只需在函数定义之前添加“virtual”即可。 - Karthik T
好的...嗯,既然它已经在我继承的CCTargetedTouchDelegate类中声明了,就像以下的虚拟函数一样:virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false; };那么我还需要在.h文件中再次声明吗?如果需要的话,是带有还是不带有virtual关键字呢? - Pawan Joshi
@DemonSOCKET,virtual 关键字需要在类定义中,在 .h 文件中。 - Karthik T

0

这个

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

定义一个独立的函数。这只能在类成员函数内部使用。要将其作为您定义的类成员,您需要限定名称:

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

-=-=-=-=

我看到大家都一起跳进来了。Objective C的语法是不同的。它使用

@implementation DragSprite
 . . . . 

@end 

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

另一个区别是,Objective C 需要通过 self 引用来调用成员函数。

[self DragSprite] ;

C++ 不

DragSprite () ;

这是在CCTargetedTouchDelegate中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做,它仍然会调用委托函数吗?还是只调用来自我的DragSprite类的函数?我的意思是,该函数是否仍然被覆盖? - Pawan Joshi
下一个问题是你要移植什么?调用委托的代码也会使用C++吗?如果是Objective C,那就有问题了。 - user3344003
好的,这并不是Objective-C语言。所谓移植,我只是在使用相同的逻辑,因为cocos2d-x和cocos2d-iPhone基本相同。如果我给您带来任何困惑,请接受我的道歉。 - Pawan Joshi
同样地,我非常新于cocos2dx和C++。所以仅仅为了一个提示,是否必须在.h文件中声明基类的任何虚函数(非纯虚)? - Pawan Joshi
是的。这是Objective C和C++之间的另一个区别。它更大。所有成员函数必须在头文件中声明。Objective C允许在没有声明的情况下在主体中定义成员。 - user3344003

0
为什么不将您的函数定义为:
bool DragSprite::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)

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


这是在CCTargetedTouchDelegate中声明的方法。我猜它是一个抽象函数。那么,如果我按照你的建议去做,它仍然会调用委托函数吗?还是只调用来自我的DragSprite类的函数?我的意思是,该函数是否仍然被覆盖? - Pawan Joshi
我可以回答这个问题,但既然你也特别询问了另外两位回答者,我会先让他们试一试。 :-) - Michael Dautermann
嗯...我并没有从那些评论中得到令人满意的答案。所以你能帮我解决这个问题吗? - Pawan Joshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接