Ok, here is what happens:
* GeometricObject.cpp includes GeometricObject.h in order to get the definition for class GeometricObject
* GeometricObject.h includes ShadeRec.h in order to get the definition of ShadeRec. Notice that at this point GeometricObject is not defined yet.
* some other include stuff along the chain
* Sphere.h tries to include GeometricObject.h, but the guarding token
__GEOMETRICOBJECT__ has been already defined and nothing happens
* class Sphere tries to derive from Geometric object, but it still has no definition. It will later, after the preprocessor returns from the ShadeRec.h include inside GeometricObject.h, but this is no use at this point
What can you do?
You can use forward declaration as you have used already in some places. Such declaration tells the compiler that you know a certain class is defined somewhere, but you disclose nothing yet about its interface and members.
You can
not do this:
1 2 3 4 5 6
|
// remove #include "GeometricObject.h"
class GeometricObject; // the fore-mentioned forward declaration
class Sphere : public GeometricObject {
...
|
, because this is inheritance, and the forward declaration provides no information on the size of the GeometricObject class, nor on the interface.
But you can do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef __GEOMETRICOBJECT__
#define __GEOMETRICOBJECT__
class RGBColor;
class ShadeRec; // you already have forward declaration
#include "Ray.h"
// optionally remove #include "ShadeRec.h"
#include "RGBColor.h"
class GeometricObject {
public:
GeometricObject(void);
GeometricObject(const GeometricObject& g);
virtual ~GeometricObject(void);
virtual bool hit(const Ray& ray, double& tmin, ShadeRec& sr) const = 0;
protected:
RGBColor color; //to be replaced
GeometricObject& operator =(const GeometricObject& rhs);
};
#endif
|
, because GeometricObject only refers to ShadeRec, it does not contain ShadeRec objects as member variables and doesn't inherit from the ShadeRec class.
It is impossible to have mutual containment, like A contains/derives from B, and B contains/derives from A. This would create infinite recursion between the definitions of those two classes.
Regards