c2054未定义基类_VS error c2504未定义基类

在C++编程中遇到VS编译错误C2504:'Item' 未定义基类。问题源于头文件包含顺序导致的闭合环状。解决方法是调整头文件的包含顺序,避免在'Item.h'中包含'ItemMushroom.h',而将其放在'Item.cpp'中包含,形成正确的包含树形结构。
摘要由CSDN通过智能技术生成

问题描述

error c2504未定义基类,编译错误,和#include头文件有关。

感谢

http://blog.csdn.net/qncj666/article/details/8562338

详细描述

(代码引自Cocos2d-x手游开发Mario)

Item.h

...

#include "ItemMushroom.h"

class Item:public CCSprite

{

...

public:

static Item* create(CCDictionary* dict);

...

}

Item.cpp

#include "Item.h"

...

Item* Item::create(CCDictionary* dict)

{

const CCString* type = dict->valueForKey("type");

if (type->m_sString == "mushroom")

{

return ItemMushroom::create(dict);

}

return NULL;

}

...

因为我在Item.cpp文件 函数static Item* create(CCDictionary* dict); 的实现中用到了ItemMushroom类的成员函数,所以在Item.h文件中 引用了ItemMushroom.h头文件。

然而我的ItemMushroom类是派生自Item

ItemMushroom.h

#include "Item.h"

class ItemMushroom:public Item

{

...

};

那么问题来了,在Item.h头文件中我包含了ItemMushroom.h,

在ItemMushroom.h头文件中又包含了Item.h,在包含的顺序上出现了闭合环状。

原因分析:

编译器首先编译Item.h,因为其包含ItemMushroom.h,引入ItemMushroom.编译,ItemMushroom继承自Item,Item尚未编译成功。此时VS2013 报错error 2504: Item 未定义基类。此错误是在编译Item.h头文件出错。

解决方法

头文件在包含顺序上不要成闭合环状,顺序结构最好应该是树。

Item.h 中删除#include "ItemMushroom.h"

Item.cpp 中加入#include "ItemMushroom.h"

Item.cpp

#include "Item.h"

#include "ItemMushroom.h"

...

Item* Item::create(CCDictionary* dict)

{

const CCString* type = dict->valueForKey("type");

if (type->m_sString == "mushroom")

{

return ItemMushroom::create(dict);

}

return NULL;

}

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值