请教一个关于DLL中C++类相互包含的问题?
我有两个类,一个表示元素,一个表示集合。
- C/C++ code
/********************** UIElementCollection.h **********************/class IUIElement;class DIRECT_UI UIElementCollection{public: UIElementCollection(); ~UIElementCollection();public: int Add(IUIElement *pUIElement); void Insert(int index, IUIElement *pUIElement); void Remove(IUIElement *pUIElement); void RemoveAt(int index); void RemoveRange(int index, int count); int IndexOf(IUIElement *pUIElement); IUIElement* operator[](int index); int Count(); void Clear(); void SetParent(IUIElement *pParent); IUIElement* GetParent();};/********************** UIElement.h **********************/#include "UIElementCollection.h"class DIRECT_UI IUIElement : public IObject{public: IUIElement(); virtual ~IUIElement();public: UIElementCollection Children;};编译错误:iuielement.h(23): error C2146: 语法错误: 缺少“;”(在标识符“Children”的前面)iuielement.h(23): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intiuielement.h(23): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
这个错误根据我个人的经验,好像是因为没有定义 UIElementCollection 的感觉,但代码中我的确是定义了啊。而在UIElement.cpp 中,我同样的定义 UIElementCollection Children 则没有问题。
我是在常规 DLL 下创建这个类的,感觉在常规 DLL 下会遇到好多非 DLL 时遇到的问题。
麻烦高手帮忙指导一下,谢谢!
[解决办法]
UIElementCollection Children;
修改成 UIElementCollection* Children; 在构造函数中new,在析构函数装那个delete
当然,.h也要加上 class UIElementCollection;声明,#include "UIElementCollection.h"转放到cpp中把。