一个类使用第二个类,该类使用第一个类中定义的结构

发布于 2024-12-01 23:44:22 字数 1013 浏览 0 评论 0原文

我正在尝试制作一个有骨头的模型。它加载 .obj 文件来创建网格,并使用面组来定义每个“骨骼”或模型的一部分。

我有一个模型课。在头文件中,我定义了一个名为 Vertex 的公共结构,它存储模型中每个顶点的位置和法线方向等数据。

我还有一个 Bone 类,它具有指向 Vertex 结构的指针向量 (#include),如下所示:
std::向量<模型::顶点* > vertices;

Model 类中将包含骨骼,因此我在标头中添加了 #included Bone 同样,Bone 类需要知道 Vertex 是什么,因此我在其标头中添加了 #included Model。
这会给 Bone 带来类型重新定义错误,因此我将 #pragma 一次放在 Bone 标头的顶部,但现在我的问题是,我收到与上面显示的代码行相关的四个编译错误。
前两个是:错误 C2653:'Model':不是类或命名空间名称
后两个是:错误C2065:'Vertex':未声明的标识符

但是我确信该行代码在语法上没有任何错误。如果我不在模型类中#include Bone,它就可以正常工作。

有谁知道如何解决此类问题?和/或有更好的方法来做到这一点吗?

编辑:我已将 class Bone; 放在模型的标头中以进行前向减速,但在模型标头的 private: 部分中,我有 Bone forearm; ,它给出了前臂使用未定义的类“Bone”的错误。
我不确定是否可以颠倒课程的顺序,例如: http://www.parashift.com/c%2B%2B-faq-lite/misc-technical-issues.html#faq-39.14,因为骨骼需要一个指向模型的指针::顶点,而不是模型本身。

I'm trying to make a model with bones. It loads a .obj file to create the mesh, and uses groups of faces to define each "bone" or part of the model.

I have a Model class. In the header file, I have defined a public struct called Vertex, which stores data such as the position and normal direction for each vertex in the model.

I also have a Bone class, which has vector (#include < vector >) of pointers to the Vertex struct, which looks like this:
std::vector< Model::Vertex* > vertices;

The Model class will have bones in it, so I have #included Bone in the header
Likewise, the Bone class needs to know what Vertex is, so I have #included Model in its header.
This gives a type redefinition error for Bone, so I have put #pragma once at the top of the Bone header, but now my problem is this I get four compile errors relating to the line of code shown above.
The first two are: error C2653: 'Model' : is not a class or namespace name
The second two are: error C2065: 'Vertex' : undeclared identifier

However I'm sure there is nothing syntactically wrong with that line of code. And if I don't #include Bone in the Model class, it works perfectly fine.

Does anybody know how to resolve this type of problem? And/or is there a better way to do this?

EDIT: I have put class Bone; in the header of Model for forward decleration, but in the private: section of the Model header, I have Bone forearm; which gives an error that forearm uses an undefined class 'Bone'.
I'm not sure if I can reverse the order of the classes such as: http://www.parashift.com/c%2B%2B-faq-lite/misc-technical-issues.html#faq-39.14, because the Bone wants a pointer to Model::Vertex, not Model itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

秋千易 2024-12-08 23:44:22

我已经把 Bone 类放在了;在用于前向声明的模型标头中,但在模型标头的 private: 部分中,我有 Bone forearm;这给出了前臂使用未定义的类“Bone”的错误。

通常前向声明用于指针/引用类型。 前臂Bone类型。因此,要实例化 forearm,它应该看到 Bone 的完整类定义。仅前向声明还不够。

// Model.h
class Bone;  // Forward declaration
class Model{
    Bone *forearm; // or Bone& forearm;
    // .....
};

// Model.cpp
#include "Bone.h"
#include "Model.h" // This inclusion order doesn't matter.

// ....

或者,您始终确保在包含 Model.h 的每个源文件中,将 Bone.h 包含在 Model.h 之前。这样,编译器的前瞻性方法就不会失败。

I have put class Bone; in the header of Model for forward declaration, but in the private: section of the Model header, I have Bone forearm; which gives an error that forearm uses an undefined class 'Bone'.

Usually forward declarations are used for pointer/reference types. forearm is of Bone type. So, for forearm to instantiate, it should see the complete class definition of Bone. Forward declaration isn't just sufficient.

// Model.h
class Bone;  // Forward declaration
class Model{
    Bone *forearm; // or Bone& forearm;
    // .....
};

// Model.cpp
#include "Bone.h"
#include "Model.h" // This inclusion order doesn't matter.

// ....

Or you always make sure that Bone.h is included before Model.h, in every source file that includes Model.h. In that way, the compiler look forward approach won't fail.

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