In my program I have classes for Vertex’s Edges and Faces which I’ll hopefully use to model shapes. Previous to this my Edge class included my “vertex.h” file and my Face class included my Edge.h file. In the Face class I declared some edge type variables and in the Edge class I declared some variables of type vertex. All working. My problem is during my implementation I realized I want edges to be aware of the faces they are joining together and I wanted to store this within edge. I wanted to declare a pointer of type Face and in the constructor to the class use:
Face * joiningFaces = new joiningFaces[2];
When I do this I get syntax errors that say that Face isn’t a type, even once I include Face.h in Edge.h includes.
Is there some sort of hierarchy system which prevents me from including Edge in Face as well as Face in Edge? or am I doing something stupid?
===Code====
===edge.h===
#ifndef EDGE_H_
#define EDGE_H_
#include "Vertex.h"
#include "Face.h"
class Edge {
private:
Vertex a;
Vertex b;
Face * joinsFace;
public:
Edge();
Edge(Vertex newa, Vertex newb);
...ect
};
===Face.h===
#ifndef FACE_H_
#define FACE_H_
#include "Edge.h"
class Face {
private:
Edge a;
Edge b;
Edge c;
public:
Face();
Face(Edge newA, Edge newB, Edge newC);
virtual ~Face();
Edge getEdgeA();
Edge getEdgeB();
Edge getEdgeC();
};
#endif /* FACE_H_ */
You have a circular reference; if you only need to refer to pointers or references to
FaceinEdge.h, then you can forward-declareFaceinstead of includingFace.h:Think about how inclusion works: if you were to paste the contents of
Face.hinEdge.h, and then the contents ofEdge.hinFace.h, you would have an infinite loop; include guards prevent multiple inclusion:But if you have classes that refer to one another, you must forward-declare one or both of them in order to break the cycle.