看书时不知代码应该添加到何处

初学者,正在阅读火烈鸟的《Cocos2d-x高级开发教程》(https://app.box.com/s/e3hbjgyvaqx4w5lh9ffb),阅读到下面截图中的段落

问题1:上面这段代码应该添加在AppDelegate.h还是AppDelegate.cpp中?

我添加在AppDelegate.cpp中后运行,有以下输出

— Begin quote from ____

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1> main.cpp
1> AppDelegate.cpp
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(7): error C2146: 语法错误: 缺少“;”(在标识符“m_msg”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(7): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(10): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(10): error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(10): error C2065: “msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(10): error C2614: “LifeCircleLogger”: 非法的成员初始化:“m_msg”不是基或成员
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(11): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(11): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(13): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(13): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(18): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(26): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(50): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(59): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1> 正在生成代码…
========== 生成: 成功 0 个,失败 1 个,最新 5 个,跳过 0 个 ==========

— End quote

我添加到AppDelegate.h中后运行,有以下报错

— Begin quote from ____

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1> main.cpp
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(38): error C2146: 语法错误: 缺少“;”(在标识符“m_msg”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(38): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2065: “msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2614: “LifeCircleLogger”: 非法的成员初始化:“m_msg”不是基或成员
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C3861: “CCLog”: 找不到标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C3861: “CCLog”: 找不到标识符
1> AppDelegate.cpp
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(38): error C2146: 语法错误: 缺少“;”(在标识符“m_msg”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(38): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2065: “msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(41): error C2614: “LifeCircleLogger”: 非法的成员初始化:“m_msg”不是基或成员
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(42): error C3861: “CCLog”: 找不到标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C2065: “m_msg”: 未声明的标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C2228: “.c_str”的左边必须有类/结构/联合
1> 类型是“‘unknown-type’”
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.h(44): error C3861: “CCLog”: 找不到标识符
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(9): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(17): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(41): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(50): error C2440: “”: 无法从“const char ”转换为“LifeCircleLogger”
1> 无构造函数可以接受源类型,或构造函数重载决策不明确
1> 正在生成代码…
========== 生成: 成功 0 个,失败 1 个,最新 5 个,跳过 0 个 ==========

— End quote

书上叫你,自己新建一个类,把代码复制到新建的类里,然后在appdelegate.cpp中include"新建的类",最后使用新建类里的日志输出方法放到appdelegate.cpp的方法中,调试程序,打印出生命周期的执行顺序。:7:

谢谢这位大哥的答复。

你意思是说,我新建一个LifeCircleLogger.h,把代码打进去吗?那LifeCircleLogger.cpp里面要写什么呢?

LifeCircleLogger.cpp 什么 都不写,因为.h已经把方法名称和内容都同时声明好了。这种写法,基础有教过,本人基础不行,不知道正确的说法很抱歉。

再次表示感谢你的热心和耐心

按照你提供的方法
我在AppDelegate.cpp第3行写了#include “LifeCircleLogger.h”

在运行时报了下面的错

— Begin quote from ____

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1> AppDelegate.cpp
1>f:\cocos2d-x-2.2.1\projects\helloworld\classes\appdelegate.cpp(3): fatal error C1083: 无法打开包括文件:“LifeCircleLogger.h”: No such file or directory
1> 正在生成代码…
1> 正在跳过…(未检测到相关更改)
1> LifeCircleLogger.cpp
========== 生成: 成功 0 个,失败 1 个,最新 5 个,跳过 0 个 ==========

— End quote

文件名我是采取复制的方式,所以可排除文件名打错的问题

你还是先学编程吧

根本不需要LifeCircleLogger.cpp这个文件

直接写在appdelegate.cpp中吗

我有PHP基础,做过几个项目
但C和C++并不懂

直接.cpp文件上就行 刚好弄过。。

感谢这位大哥的帮助

我现在就是直接在AppDelegate.cpp中添加LifeCircleLogger类
但也就出现了我在主帖中出现的第一段报错输出

尤其是其中说““m_msg”: 未声明的标识符”让我很困惑

下面是我的代码截图,还望能帮忙指正我这个新人

名称空间 using namespace std;
你还是先看看c++吧

原来是这样~~感谢
已经下载了一些C++课程来看了。

初学者,一些东西没有感念,学习也没有找到合适的方法,所以提问了弱智的问题,占用大家宝贵时间,非常抱歉。

再次表示感谢!

衷心的!