在 C++包括这样的方式有什么问题吗?
在我的程序中,我有顶点的边和面的类,我希望用它们来建模形状。在此之前,我的 Edge 类包含我的“vertex.h”文件,我的 Face 类包含我的 Edge.h 文件。在 Face 类中,我声明了一些边类型变量,在 Edge 类中我声明了一些顶点类型变量。一切都在工作。我的问题是在我的实现过程中,我意识到我希望边缘能够意识到它们连接在一起的面,并且我想将其存储在边缘中。我想声明一个 Face 类型的指针,并在类的构造函数中使用:
Face * joiningFaces = new joiningFaces[2];
当我这样做时,我会收到语法错误,指出 Face 不是类型,即使我将 Face.h 包含在 Edge.h 中也是如此。
是否存在某种层次结构阻止我将“边”包含在“面”以及“面”包含在“边”中?或者我在做一些愚蠢的事情?
===代码====
===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_ */
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_ */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有一个循环引用;如果您只需要在
Edge.h
中引用Face
的指针或引用,那么您可以前向声明Face
而不是包含Face.h
:考虑包含的工作原理:如果您要将
Face.h
的内容粘贴到Edge.h
中,然后将Edge.h
中Face.h
,你会遇到无限循环; include 守卫可防止多重包含:但是,如果您有相互引用的类,则必须前向声明其中一个或两个以打破循环。
You have a circular reference; if you only need to refer to pointers or references to
Face
inEdge.h
, then you can forward-declareFace
instead of includingFace.h
:Think about how inclusion works: if you were to paste the contents of
Face.h
inEdge.h
, and then the contents ofEdge.h
inFace.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.
如果要在边缘类中声明一个指向Face的指针,编译器需要声明Face。如果实例化 Face 对象,编译器需要 Face 类的定义。
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.
从技术上讲,您应该对头文件中所有具有
.h
扩展名的类进行原型设计,然后将代码写入相应的.cpp
文件中。如果一个类需要引用另一个类,只需将包含文件添加到所引用的类的.h
中即可。例如,如果face指的是edge,则将edge.h
添加到face.h
。不要在main.cpp(或 main 函数所在的位置)
中重新包含edge.h
。您还可以选择将代码写入'.h'
文件本身。选择权在你。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 addedge.h
toface.h
. Do not re-includeedge.h
inmain.cpp (or wherever the main function is)
. You could also choose to write the code in the'.h'
file itself. The choice is yours.