首页 > 其他 > 详细

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

时间:2014-12-08 00:56:07      阅读:285      评论:0      收藏:0      [点我收藏+]

        今天写程序遇到了一个问题,花了好几个小时各种找资料都没有解决,终于皇天不负有心人还是让我给把它kill 掉了。什么问题呢?那就是头文件相互包含出错(当然之前我并不知道是这个原因),先来看代码:

Test1.h

#include "cocos2d.h"
#include "Test2.h"

class Test1 : public Layer
{
    …………
    Test2 *test2;
};

Test2.h

#include "cocos2d.h"
#include "Test1.h"

class Test2 : public Layer
{
    …………
    Test1 *test1;
};

        如果你的代码有与上代码类似的写法,那么在编译时就会因头文件相互包含出错,Windows平台下会提示:

bubuko.com,布布扣

Mac平台下会提示:

bubuko.com,布布扣


解决办法:

Test1.h

#include "cocos2d.h"

class Test2;        //只是告诉编译器,需要这个类,其他功能结构等都没

class Test1 : public Layer
{
    …………
    Test2 *test2;
};

Test1.cpp

#include "Test1.h"
#include "Test2.h"        //真正的包含
………………

Test2.h

#include "cocos2d.h"

class Test1;        //只是告诉编译器,需要这个类,其他功能结构等都没

class Test2 : public Layer
{
    ……………
    Test1 *test1;
};

Test2.cpp

#include "Test2.h"
#include "Test1.h"        //真正的包含
………………

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

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

原文:http://my.oschina.net/Jacedy/blog/353639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!