头文件相互包含出错问题解决

        今天写程序遇到了一个问题,花了好几个小时各种找资料都没有解决,终于皇天不负有心人还是让我给把它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平台下会提示:

234720_jbdJ_580523.png

Mac平台下会提示:

235320_6Icv_580523.jpg


解决办法:

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"        //真正的包含
………………

这样修改后,问题就解决啦啦啦。。。哎,希望大家不要出现这样的错误。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值