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

3

9

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.

Arnulfoarny answered 5/5, 2014 at 6:21 Comment(4)
isn't the error message clear enough? you need DragSprite::Kistler
You are missing the class scope in the bool ccTouchBegan definition.Voile
@BryanChen : ok i get it, but how should i use the class pointer. actually i am porting code from Objective-C. and there has been use of "self" ?Arnulfoarny
@DemonSOCKET There are a few places that an explicit this is required in C++, in most other places (including here) it is considered bad style.Chronaxie
C
22
bool ccTouchBegan(

needs to be

bool DragSprite::ccTouchBegan(

You shouldnt need this in the first place.

Chronaxie answered 5/5, 2014 at 6:24 Comment(4)
its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ?Arnulfoarny
@DemonSOCKET hmm good question, I think your porting is missing one more element. In C++ virtual functions are opt-in, unlike obj-c. You want to use virtual to make this method a "virtual" function, then it will work as in obj-c. en.wikipedia.org/wiki/Virtual_function for more info. Just add virtual before the function definitionChronaxie
OK.. Well, since it is already declared in CCTargetedTouchDelegate class from which i am inheriting, as of like the following virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false; }; Do i still need to declare this in .h file ? if yes then with or without virtual keyword ?Arnulfoarny
@DemonSOCKET the virtual needs to be in the class definition, in the .h fileChronaxie
J
0

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.

Jurkoic answered 5/5, 2014 at 6:24 Comment(3)
its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ?Arnulfoarny
I'd answer this question, but since you've also specifically asked the other two people who answered your question also I'll give them a shot first. :-)Jurkoic
well... i am not getting any satisfactory answer with those comments. so would you please help me out here ?Arnulfoarny
C
0

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. 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

[self DragSprite] ;

C++ does not

DragSprite () ;
Cowage answered 5/5, 2014 at 6:26 Comment(5)
its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ?Arnulfoarny
The next question is what are you porting? Is the code calling the delegate going to be in C++ too? If it's objective C, that's going to be a problem.Cowage
well.. no its not in the objective-C. by porting i mean i am just using the logic here. because cocos2d-x and cocos2d-iPhone are mostly same. Apologies, if i caused you any confusions.Arnulfoarny
also .. i am very much new to cocos2dx and c++. so just for a hint, is it a must to declare any virtual function(not pure) of base class in .h file too ?Arnulfoarny
Yes. This is another difference between objective C and C++. It's larger. All member functions must be declared in the header. Objective C allows members to be defined in the body without a declaration.Cowage

© 2022 - 2024 — McMap. All rights reserved.