C++ 抽象类,该类具有随后定义的数据类型的方法,该方法具有指向该类的指针。四处走动

C++ 抽象类,该类具有随后定义的数据类型的方法,该方法具有指向该类的指针。四处走动,c++,C++,我试图实现一个抽象类“Primitive”,定义为 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; }; 我的问题是,原语包含一个方法i

我试图实现一个抽象类“Primitive”,定义为

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;
};
我的问题是,原语包含一个方法intersect,该方法使用定义为

class Intersection
{
    LocalGeo localGeo;
    Primitive* primitive;
};
交叉点具有到基本体的链接。所以,我无法编译它,因为编译器给出了一个错误,即在原语定义之后没有定义交集

问题归结为

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


class B
{
    A *a;   
}
有没有办法用这种方式定义类?我试着用谷歌搜索,但我不知道该用谷歌搜索什么


谢谢

您需要转发声明类
B

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

应修复编译。

在头文件中使用转发声明:

class Intersection; // forward declaration

class Primitive { /* as before */ };


前方申报
交叉口
。谢谢。它解决了这个问题。
class Primitive; // forward declaration

class Intersection { /* as before */ };