2013-04-27 13 views
1

我想实现一个抽象类“原始”定义为带有Method的抽象类,后面定义了具有指向Class的指针的数据类型。去

class Primitive 
{ 
    virtual bool intersect(Ray& ray, float* thit, Intersection* in) = 0; 
    virtual bool intersectP(Ray& ray) = 0; 
    virtual void getBRDF(LocalGeo& local, BRDF* brdf) = 0; 
}; 

我的问题是原始包含方法交叉使用,其定义为

class Intersection 
{ 
    LocalGeo localGeo; 
    Primitive* primitive; 
}; 

路口有交集型链接到原始。所以,我不能编译这个,因为编译器给出了一个错误,即Intersection没有被定义,因为它在Primitive定义之后。

的问题归结为...

class A 
{ 
    void afunc(B *b); 
}; 


class B 
{ 
    A *a; 
} 

是否有办法以这种方式来定义类?我试图谷歌,但我不知道什么谷歌。

感谢

+0

正向声明'Intersection'。 – lapk 2013-04-27 22:51:37

回答

2

使用前置声明:

class Intersection; // forward declaration 

class Primitive { /* as before */ }; 

class Primitive; // forward declaration 

class Intersection { /* as before */ }; 
+0

谢谢。这工作。 – AnilMS 2013-04-28 06:17:57

0

您需要转发声明类B

class B; 
class A 
{ 
    void afunc(B *b); 
}; 

应该修复编译。在你的头文件

+0

谢谢。它解决了这个问题。 – AnilMS 2013-04-28 06:18:18

相关问题