我正在尝试实现一个定义为“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;
};
我的问题是 Primitive 包含一个使用定义为 Intersection 类型的 Method intersect
class Intersection
{
LocalGeo localGeo;
Primitive* primitive;
};
Intersection 有指向 Primitive 的链接。因此,我无法编译它,因为编译器给出了一个错误,即交集未定义,因为它位于原始定义之后。
问题归结为...
class A
{
void afunc(B *b);
};
class B
{
A *a;
}
有没有办法这样定义类?我尝试用谷歌搜索,但不确定要用谷歌搜索什么。
谢谢
最佳答案
在头文件中使用前向声明:
class Intersection; // forward declaration
class Primitive { /* as before */ };
和
class Primitive; // forward declaration
class Intersection { /* as before */ };
关于c++ - 具有方法的抽象类,其后定义了数据类型,它具有指向该类的指针。四处走动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257651/