• 5970阅读
  • 3回复

头文件需要互相包含的怎么办? [复制链接]

上一主题 下一主题
离线sd菜
 
只看楼主 倒序阅读 楼主  发表于: 2010-09-28
— 本帖被 XChinux 执行加亮操作(2010-10-21) —
我分开写的几个widget类,为了方便互相包含我都写在一个.h里面了,但是要在一个窗口里点击一个按钮弹出另一个窗口,需要在类中声明另一个类的对象,但是互相都写在一个文件里,怎么互相包含啊,我试过直接在各自的类中互相声明另一个类的对象,出错了:

start.h:54: error: 'Math_all' does not name a type

start.h:96: error: 'Math_exm' does not name a type
请问我该怎么来包含呢?
离线steinlee

只看该作者 1楼 发表于: 2010-09-29
in class A header A.h write:

class B;

class A
{
public:
     B b;
};

in class B header B.h write:

class A;

class B
{
public:
     A a;
};

This is often used in friend class definition. Always remember that something has to be defined before it is used. Try to define classes in separate headers.
Looking for remote C/C++ and Qt 兼职
离线love0105

只看该作者 2楼 发表于: 2010-09-29
可能设计的不太合理,可以修改设计避免这种情况,或者不要在头文件中包含,在cpp中包含,比如A类头文件包含B.h,如果B中需要A的话,在B.cpp中去包含A.h
离线80486

只看该作者 3楼 发表于: 2010-09-29
两个类相互依赖那就是有问题了。
或者你可以保留指针,用前置声明
快速回复
限100 字节
 
上一个 下一个