今天写程序遇到了一个问题,花了好几个小时各种找资料都没有解决,终于皇天不负有心人还是让我给把它kill 掉了。什么问题呢?那就是头文件相互包含出错(当然之前我并不知道是这个原因),先来看代码:
Test1.h
1
2
3
4
5
6
7
8
|
#include "cocos2d.h"
#include "Test2.h"
class
Test1 :
public
Layer
{
…………
Test2 *test2;
};
|
Test2.h
1
2
3
4
5
6
7
8
|
#include "cocos2d.h"
#include "Test1.h"
class
Test2 :
public
Layer
{
…………
Test1 *test1;
};
|
如果你的代码有与上代码类似的写法,那么在编译时就会因头文件相互包含出错,Windows平台下会提示:
Mac平台下会提示:
解决办法:
Test1.h
1
2
3
4
5
6
7
8
9
|
#include "cocos2d.h"
class
Test2;
//只是告诉编译器,需要这个类,其他功能结构等都没
class
Test1 :
public
Layer
{
…………
Test2 *test2;
};
|
Test1.cpp
1
2
3
|
#include "Test1.h"
#include "Test2.h" //真正的包含
………………
|
Test2.h
1
2
3
4
5
6
7
8
9
|
#include "cocos2d.h"
class
Test1;
//只是告诉编译器,需要这个类,其他功能结构等都没
class
Test2 :
public
Layer
{
……………
Test1 *test1;
};
|
Test2.cpp
1
2
3
|
#include "Test2.h"
#include "Test1.h" //真正的包含
………………
|
这样修改后,问题就解决啦啦啦。。。哎,希望大家不要出现这样的错误。