c++ - 使用 .inl 内联文件的不完整前向声明
问题描述
我希望标题没有误导。
我正在制作自己的线性代数数学库,只是为了练习和更好地理解数学编程。我使用glm
数学库作为参考和帮助。我现在的类型是:
class Vector2, Vector3, Vector4
所有类都代表浮点向量(稍后将是template
d)。
矢量2.h
#include <SomeGenericMathFunctions.h>
//Forward Declare Vector3
struct Vector3;
struct Vector2
{
public:
float X, Y;
//Constructors
//Constructor in question
Vector2(const Vector3 & inVec3);
//Functions
//Operators
};
#include <Vector2.inl>
Vector2.inl
//Inline Constructor definitions
.
.
.
//Constructor in question
inline Vector2::Vector2(const Vector3 & inVec3) : X(inVec3.X), Y(inVec3.Y)
{}
//Functions & operators definitions
Vector3
是后来定义的。这段代码给了我use of undefined type 'Vector3'
. 据我所知glm
,正在做同样的事情,一切看起来都很好(glm
不包括vec3
里面的任何地方vec2
)。这是一个有用的链接,它帮助我更好地理解正在发生的事情,看起来它说的是同样的事情,单独的声明/定义等。
我使用 VS Code Maps 对 & 的包含和依赖项进行了扩展搜索,glm
但我找不到任何东西。我错过了什么?vec2
vec3
编辑:我主要关心的是如何glm
做我的代码试图做的事情。我已经知道“简单/正确”的方式,但我想了解glm
's 的代码。
我正在使用c++11
+
解决方案
据我搜索和理解glm
使用前向声明。type_vec.hpp
每个type_vecX.hpp
文件中都包含声明,并且typedefs
对于所有向量(float
- bool
,high
-low
精度)line 103
。
诀窍是使用template
s. 首先我template
知道structs
template<typename T>
struct Vector2
{
public:
...
};
对于有问题的构造函数,变化是
宣言:
template<typename S>
Vector2(const Vector3<S> & inVec3);
定义
template <typename T>
template <typename S>
inline Vector2<T>::Vector2(const Vector3<S> & inVec3) :
X(static_cast<T>(inVec3.X)),
Y(static_cast<T>(inVec3.Y))
{}
推荐阅读
- r - 如何在R中绑定矩阵列表的所有行组合
- javascript - 如何只导入我需要的功能和摇树
- php - PHP 函数返回 ="" 而不是 /
- php - 如果属性具有空字符串值,如何使 Doctrine 拒绝实体?
- kubernetes-helm - 检查数组是否包含属性等于特定值的对象
- javascript - 如何将元素的值保存在 React 的状态中?
- .net - 如何使用 Windows 身份验证修复 ASP.NET Core 应用程序中的太多重定向错误?
- kubernetes - Kubeflow 管道终止通知
- reactjs - 如何在根/父条目中使用 useContext 访问上下文数据?
- c# - 如何调试 Azure EventHubs?