cocos2d-x 头文件中添加方法变量导致编译报错

时间:2022-11-19 11:20:02

代码如下:

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

// a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    void addMonster(float dt);
    void GameUpdate(float);
    // implement the "static create()" method manually
    virtual bool onTouchBegan(Touch* pTouch, Event* pEvent);
    virtual void onTouchMoved(Touch* pTouch, Event* pEvent);
    virtual void onTouchEnded(Touch* pTouch, Event* pEvent);

bool onContactBegin(const PhysicsContact& contact);

CREATE_FUNC(HelloWorld);
private:
    std::vector<cocos2d::Sprite*> Monster;
    std::vector<cocos2d::Sprite*> projectile;
    EventListenerTouchOneByOne* touch_listener;
    EventListenerPhysicsContact* contact_listener;
    Sprite* _player;
    int i, j;
};

#endif // __HELLOWORLD_SCENE_H__

报错:

Error    4    error C2061: syntax error : identifier 'Touch' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    19    1    hello
Error    6    error C2061: syntax error : identifier 'Touch' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    20    1    hello
Error    8    error C2061: syntax error : identifier 'Touch' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    21    1    hello
Error    3    error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    19    1    hello
Error    5    error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    20    1    hello
Error    7    error C2061: syntax error : identifier 'Touch' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    21    1    hello
Error    37    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    84    1    hello
Error    38    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    85    1    hello
Error    40    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    86    1    hello
Error    53    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    166    1    hello
Error    56    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    168    1    hello
Error    59    error C2065: '_player' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    169    1    hello
Error    25    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    45    1    hello
Error    26    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    46    1    hello
Error    28    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    47    1    hello
Error    30    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    48    1    hello
Error    32    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    49    1    hello
Error    34    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    50    1    hello
Error    36    error C2065: 'touch_listener' : undeclared identifier    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    51    1    hello
Error    12    error C2143: syntax error : missing ',' before '&' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    23    1    hello
Error    11    error C2143: syntax error : missing ',' before '&' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    23    1    hello
Error    14    error C2143: syntax error : missing ';' before '*' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    30    1    hello
Error    18    error C2143: syntax error : missing ';' before '*' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    31    1    hello
Error    22    error C2143: syntax error : missing ';' before '*' (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    32    1    hello
Error    13    error C2143: syntax error : missing ';' before '*' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    30    1    hello
Error    17    error C2143: syntax error : missing ';' before '*' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    31    1    hello
Error    21    error C2143: syntax error : missing ';' before '*' (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    32    1    hello
Error    50    error C2227: left of '->addChild' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    165    1    hello
Error    57    error C2227: left of '->getContentSize' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    168    1    hello
Error    54    error C2227: left of '->getPosition' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    166    1    hello
Error    60    error C2227: left of '->getPosition' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    169    1    hello
Error    29    error C2227: left of '->onTouchBegan' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    47    1    hello
Error    35    error C2227: left of '->onTouchCancelled' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    50    1    hello
Error    33    error C2227: left of '->onTouchEnded' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    49    1    hello
Error    31    error C2227: left of '->onTouchMoved' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    48    1    hello
Error    43    error C2227: left of '->removeChild' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    138    1    hello
Error    45    error C2227: left of '->removeChild' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    139    1    hello
Error    63    error C2227: left of '->runAction' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    173    1    hello
Error    39    error C2227: left of '->setPosition' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    85    1    hello
Error    27    error C2227: left of '->setSwallowTouches' must point to class/struct/union/generic type    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    46    1    hello
Error    51    error C2228: left of '.back' must have class/struct/union    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    165    1    hello
Error    62    error C2228: left of '.back' must have class/struct/union    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    173    1    hello
Error    48    error C2228: left of '.push_back' must have class/struct/union    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    164    1    hello
Error    58    error C2228: left of '.width' must have class/struct/union    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    168    1    hello
Error    61    error C2228: left of '.y' must have class/struct/union    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    169    1    hello
Error    41    error C2511: 'bool HelloWorld::onContactBegin(const cocos2d::PhysicsContact &)' : overloaded member function not found in 'HelloWorld'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    134    1    hello
Error    46    error C2511: 'bool HelloWorld::onTouchBegan(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'HelloWorld'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    151    1    hello
Error    65    error C2511: 'void HelloWorld::onTouchEnded(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'HelloWorld'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    183    1    hello
Error    64    error C2511: 'void HelloWorld::onTouchMoved(cocos2d::Touch *,cocos2d::Event *)' : overloaded member function not found in 'HelloWorld'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    178    1    hello
Error    47    error C2597: illegal reference to non-static member 'HelloWorld::j'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    157    1    hello
Error    52    error C2597: illegal reference to non-static member 'HelloWorld::j'    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    165    1    hello
Error    55    error C2660: 'cocos2d::ccpSub' : function does not take 1 arguments    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    166    1    hello
Error    42    error C2671: 'HelloWorld::onContactBegin' : static member functions do not have 'this' pointers    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    138    1    hello
Error    44    error C2671: 'HelloWorld::onContactBegin' : static member functions do not have 'this' pointers    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    139    1    hello
Error    49    error C2671: 'HelloWorld::onTouchBegan' : static member functions do not have 'this' pointers    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    165    1    hello
Error    10    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    23    1    hello
Error    16    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    30    1    hello
Error    20    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    31    1    hello
Error    24    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    32    1    hello
Error    9    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    23    1    hello
Error    15    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    30    1    hello
Error    19    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    31    1    hello
Error    23    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\classes\helloworldscene.h    32    1    hello
Warning    66    warning C4018: '<' : signed/unsigned mismatch    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    190    1    hello
Warning    67    warning C4018: '<' : signed/unsigned mismatch    e:\cocos2d-x\samples\hello\classes\helloworldscene.cpp    192    1    hello
Warning    2    warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss (..\Classes\AppDelegate.cpp)    e:\cocos2d-x\samples\hello\cocos2d\cocos\physics\ccphysicsjoint.h    1    1    hello
Warning    1    warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss (..\Classes\HelloWorldScene.cpp)    e:\cocos2d-x\samples\hello\cocos2d\cocos\physics\ccphysicsjoint.h    1    1    hello

解决方法:

在行#include "cocos2d.h"下增加如下代码行:
USING_NS_CC;