在 C++包括这样的方式有什么问题吗?

发布于 2024-12-10 05:58:42 字数 1029 浏览 0 评论 0原文

在我的程序中,我有顶点的边和面的类,我希望用它们来建模形状。在此之前,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-12-17 05:58:42

你有一个循环引用;如果您只需要在Edge.h中引用Face的指针或引用,那么您可以前向声明Face而不是包含Face.h

class Face;

考虑包含的工作原理:如果您要将 Face.h 的内容粘贴到 Edge.h 中,然后将Edge.hFace.h,你会遇到无限循环; include 守卫可防止多重包含:

#ifndef X_H
#define X_H

// ...

#endif

但是,如果您有相互引用的类,则必须前向声明其中一个或两个以打破循环。

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.

情绪 2024-12-17 05:58:42

如果要在边缘类中声明一个指向Face的指针,编译器需要声明Face。如果实例化 Face 对象,编译器需要 Face 类的定义。

// 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

....

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

....
帅冕 2024-12-17 05:58:42

从技术上讲,您应该对头文件中所有具有 .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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文