简体   繁体   English

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

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

I am using CCTouchTargetedDelegate with a Class subclassed by CCSprite. 我正在使用CCTouchTargetedDelegate和CCSprite子类。 when defining delegate methods i am not being able to use "this" inside the functions. 在定义委托方法时,我无法在函数内部使用“this”。

as answered on previously asked questions I couldn't used name of class with the function using scope resolution , because it then gave me error of "Out-of-line definition of 'ccTouchBegan' does not match any declaration in 'mygames::DragSprite'" 正如先前提到的问题所回答的那样,我无法使用具有使用范围分辨率的函数的类的名称,因为它然后给了我“'ccTouchBegan'的外线定义与'mygames :: DragSprite中的任何声明都不匹配的错误“”

I also tried to declare the function in .h file but nothing seems to work. 我也尝试在.h文件中声明该函数,但似乎没有任何效果。

My code is as below :- 我的代码如下: -

.h File .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 .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;
}

The Error Screen Shot :- 错误屏幕截图: -

What am i missing here ? 我在这里失踪了什么?

JUST OUT OF CURIOSITY 只是出于好奇心

AS suggested in the answers, If i use AS在答案中建议,如果我使用

bool DragSprite::ccTouchBegan

So, Would it still call the delegete function ? 那么,它还会调用delegete函数吗? or just the function from my DragSprite class. 或者只是我的DragSprite类中的函数。 I mean, would the function be still overridden ? 我的意思是,该功能是否仍会被覆盖? well... its the method declared in CCTargetedTouchDelegete. 嗯...它是在CCTargetedTouchDelegete中声明的方法。 and i guess its an abstract function. 我猜它是一个抽象的函数。

bool ccTouchBegan(

needs to be 需要是

bool DragSprite::ccTouchBegan(

You shouldnt need this in the first place. 你不应该首先需要this

Why not define your function as 为什么不将您的功能定义为

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

which is how you have it defined in the DragSprite class in your .h file. 这是你在.h文件中的DragSprite类中定义它的方法。

This 这个

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

defines a stand alone function. 定义了一个独立的功能。 this can only be used within a class member function. 这只能在类成员函数中使用。 To make it a class member as you have defined it, you need to qualify the name: 要使其成为已定义的类成员,您需要限定名称:

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

-=-=-=-= - = - = - = - =

I see everyone jumped in at once. 我看到每个人都立刻跳了进来。 The syntax of objective C is different. 目标C的语法不同。 It uses 它用

@implementation DragSprite
 . . . . 

@end 

to specify the class and requires a - to indicate a non static member function. 指定类并要求 - 表示非静态成员函数。

Another difference is that Objective C requires self referencing to call a member function 另一个区别是Objective C需要自引用来调用成员函数

[self DragSprite] ;

C++ does not C ++没有

DragSprite () ;

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

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