【发布时间】:2017-08-12 00:52:14
【问题描述】:
我有一个基类Shape,它有一个虚函数intersect()。
HitRecord 是在同一 .h 文件中定义的结构。
另外,Shape 有一个子类 Triangle。我正在尝试在Shape::intersect() 中访问HitRecord 的成员,但出现错误error: member access into incomplete type in base class virtual function
奇怪的是,我可以在子类中做到这一点,但在基类中却不能。
是不是因为它是一个虚拟函数?
注意:另一个奇怪的事情:我可以在我的 Ubuntu 16.04 上运行,但在我的 mac 上遇到这个错误。
代码
struct HitRecord; // forward declaration
class Shape {
public:
virtual bool intersect(Ray& r, HitRecord& rec) {
std::cout << "Child intersect() is not implement." << std::endl;
rec.obj = this;
return false;
}
}
struct HitRecord {
float t;
vec3f p; // point coord
vec3f norm;
Shape* obj;
};
class Triangle: public Shape {
public:
Mesh* mesh_ptr;
unsigned int vertexIndex[3];
Triangle() {...}
Triangle(Mesh* m) {...}
inline bool intersect(Ray& r, HitRecord& rec);
}
inline bool Triangle::intersect(Ray& r, HitRecord& rec) {
vec3f n = cross(v1-v0, v2-v0);
float t = - (dot(n, r.origin())+d) / dot(n, r.direction());
vec3f p = r.origin() + t*r.direction();
rec.t = t;
rec.p = p;
rec.norm = unit(n);
rec.obj = this;
return true;
}
【问题讨论】:
标签: c++