一个类使用第二个类,该类使用第一个类中定义的结构
我正在尝试制作一个有骨头的模型。它加载 .obj 文件来创建网格,并使用面组来定义每个“骨骼”或模型的一部分。
我有一个模型课。在头文件中,我定义了一个名为 Vertex 的公共结构,它存储模型中每个顶点的位置和法线方向等数据。
我还有一个 Bone 类,它具有指向 Vertex 结构的指针向量 (#includestd::向量<模型::顶点* > 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常前向声明用于指针/引用类型。
前臂
是Bone
类型。因此,要实例化forearm
,它应该看到Bone
的完整类定义。仅前向声明还不够。或者,您始终确保在包含
Model.h
的每个源文件中,将Bone.h
包含在Model.h
之前。这样,编译器的前瞻性方法就不会失败。Usually forward declarations are used for pointer/reference types.
forearm
is ofBone
type. So, forforearm
to instantiate, it should see the complete class definition ofBone
. Forward declaration isn't just sufficient.Or you always make sure that
Bone.h
is included beforeModel.h
, in every source file that includesModel.h
. In that way, the compiler look forward approach won't fail.