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

代码如下:

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;

时间: 2025-01-11 22:36:09

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

Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法

Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, errno=2 [No such file or directory]. Using an empty file 解决方法: 1.先执行一次场景,让其报错,主要是为了创建完整的C:\Users\Administrator\AppData\Local\Temp\brr_WJy.395目录下的子目录和文件 2.在

Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488

Error -26488: Could not obtain information about submitted file "E:.jpg": _stat32 rc=-1, errno=2 [No such file or directory]. Using an empty file解决方法: 1.先执行一次场景,让其报错,主要是为了创建完整的C:\Users\Administrator\AppData\Local\Temp\brr_WJy.395目录下的子目录和文件2.在192

Xcode8中添加Swift自动布局SnapKit框架报错,编译失败

---恢复内容开始--- 既然SnapKit的作者说SnapKit已经支持Swift3.0了,那么我们就先来适配SnapKit,首先用Xcode8新建一个空项目,利用Cocoapods导入SnapKit. Podfile 打开工程,依然弹出这个选项: 是否转换到Swift3.0 刚才选择了Convert依然报错,可见不靠谱,这次我们全部选择Later. 编译后,报错: 报错 错误提示我们依然是"Use Legacy Swift Language Version"这个选项的问题.我们来看

pom.xml中添加oracle数据库驱动包报错: Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0

pom.xml 加载jar包时报错:Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0. 由于oracle的ojdbc收费,所以在maven项目导入时没有ojdbc14-10.2.0.4.0.jar包. 解决方法: 配置本地库.下载jar包,把jar包放在仓库对应的文件夹ojdbc14\10.2.0.4.0\里,安装到本地仓库(需要事先配过maven的环境变量),在命令行工具中写如下命令,注意本地仓库位置一定要写对: mvn install:in

[C/C++]在头文件中使用static定义变量意味着什么

文章出处:http://www.cnblogs.com/zplutor/ 看到有一位同学在头文件中这么写: 1 static const wchar_t* g_str1 = - 2 static const wchar_t* g_str2 = - 这种定义变量的方式我从来没有见过,而且它还能顺利通过编译,于是我很想知道编译器是如何处理这种变量定义的. 定义全局变量时使用static,意味着该变量的作用域只限于定义它的源文件中,其它源文件不能访问.既然这种定义方式出现在头文件中,那么可以很自然地推

将函数实现放在头文件中

研究一个开源算法库,采用C++模板编程,所有函数实现都放在了头文件中,现在把模板去掉,链接时发生冲突,具体原因如下: 因为多个源文件包含了含有函数定义的头文件,在编译的时候,每个源文件都会包含一份函数定义,在链接时编译器不知道需要连接哪一份函数定义. 解决方法有三个: 1. inline关键字 可以用关键字inline修饰函数定义,例如: inline int add(int x, int y) { return x + y; } 使用inline关键字,编译器会在调用此函数的地方把函数的目标代

c语言头文件中定义变量

最近在看一个PHP的扩展源码,编译的时候的遇到一个问题: ld: 1 duplicate symbol for architecture x86_64 仔细看了一下源码,发现在头文件中 出现了全局变量的定义 ZEND_DECLARE_MODULE_GLOBALS(xx) 简单开来,可以这么理解 // t1.h #ifndef T1_H #define T1_H int a = 0; #endif //------------------ //t1.c #include "t1.h" #

头文件中不可以放变量的定义

注意头文件中不可以放变量的定义!!!一般情况下头文件中只放变量的声明,因为头文件 要被其他文件包含(即#include),如果把定义放到头文件的话,就不能避免多次定义变量, C++不允许多次定义变量,一个程序中对指定变量的定义只有一次,声明可以无数次. 不过有三个例外,一下三中实体的定义也可放到头文件中 1.值在编译时就已知的const 变量的定义可以放到头文件中 如:const int num(10); 2.类的定义可以放到头文件中 3.inline 函数 4.C++11的新特性 conste

关于在头文件中定义变量的问题

如果在一个头文件中定义了一个变量 A.h: int i; 在A.cpp,B.cpp...文件中包含了这个头文件,编译时就会产生重复定义的错误.问题所在是因为int i;这句代码是定义了一个i变量,包含一次头文件就定义了一次i,包含多次肯定引起错误.如果改成extern int i;这句代码就变成了一个声明了.并不会定义i这个变量.不过在多个cpp文件里初始化一样会出现错误.只能在一个cpp文件中进行初始化.