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

Pawan Joshi picture Pawan Joshi · May 5, 2014 · Viewed 23.1k times · Source

I am using CCTouchTargetedDelegate with a Class subclassed by CCSprite. when defining delegate methods i am not being able to use "this" inside the functions.

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'"

I also tried to declare the function in .h file but nothing seems to work.

My code is as below :-

.h File

#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;
}

The Error Screen Shot :-

What am i missing here ?

JUST OUT OF CURIOSITY

AS suggested in the answers, If i use

bool DragSprite::ccTouchBegan

So, Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ? well... its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function.

Answer

Karthik T picture Karthik T · May 5, 2014
bool ccTouchBegan(

needs to be

bool DragSprite::ccTouchBegan(

You shouldnt need this in the first place.