开发者

In C++ is there something wrong including in this way?

开发者 https://www.devze.com 2023-04-13 00:54 出处:网络
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

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 FAC开发者_如何学编程E_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 Face in Edge.h, then you can forward-declare Face instead of including Face.h:

class Face;

Think about how inclusion works: if you were to paste the contents of Face.h in Edge.h, and then the contents of Edge.h in Face.h, you would have an infinite loop; include guards prevent multiple inclusion:

#ifndef X_H
#define X_H

// ...

#endif

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.


If you want to declare a pointer to Face in the edge class, the compiler needs a declaration of Face. If you instantiate the Face object, the compiler needs the definition of the Face class.

// edge.hpp
class Face;  // forward declaration
class Edge
{
public:
  Edge(Face* myFace);
private:
  Face* myFace_;
};

// edge.cpp
#include face.hpp  // <- include the Face definition here

....


Technically, you should prototype all classes in the header file with <name>.h extension and then write the code in the corresponding .cpp file. If a class needs to refer to another class, only add the include file to the .h of the class which is referring. For example if face refers to edge, then add edge.h to face.h. Do not re-include edge.h in main.cpp (or wherever the main function is). You could also choose to write the code in the '.h' file itself. The choice is yours.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号