2015-01-04 79 views
0

我声明两个类,如下面的静态实例,A是父类,B子答:父类包含子类C++

//a.h 
#include "b.h" 
//class B;  Adding this line doesn't work 
class A{ 
    static B b; 
} 

//b.h 
#include "a.h" 
class B:public A{  // XCode error here: expected class name 

} 

然而,的XCode 6.1别让我编译一直在说“预期班级名称”。

实际上,我试图实现一本书中提到的状态机游戏编程模式http://gameprogrammingpatterns.com/state.html#static-states。在该书中,父状态类拥有子类的静态实例。

+1

您的设计需要除臭剂。 – 2015-01-04 13:54:06

+1

前向声明'class B;'很好。通知不包括。刮'#包括“b.h”' – 2015-01-04 13:57:54

回答

3

下面的代码就足够你: -

//b.h 
#include "a.h"  <<<< This requires full definition for `A`. 
class B : public A 
{ 

} 

//a.h   <<<<< No need to include any file. 
class B; 
class A{ 
    static B b; 
}; 
+0

非常感谢。添加包含文件已成为我的第二个本质。 – shapeare 2015-01-04 14:01:35