首页 > 解决方案 > 使用 .inl 内联文件的不完整前向声明

问题描述

我希望标题没有误导。

我正在制作自己的线性代数数学库,只是为了练习和更好地理解数学编程。我使用glm数学库作为参考和帮助。我现在的类型是:

class Vector2, Vector3, Vector4

所有类都代表浮点向量(稍后将是templated)。

矢量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但我找不到任何东西。我错过了什么?vec2vec3

编辑:我主要关心的是如何glm做我的代码试图做的事情。我已经知道“简单/正确”的方式,但我想了解glm's 的代码。

我正在使用c++11+

标签: c++c++11c++14glm-math

解决方案


据我搜索和理解glm使用前向声明。type_vec.hpp每个type_vecX.hpp文件中都包含声明,并且typedefs对于所有向量(float- boolhigh-low精度)line 103

诀窍是使用templates. 首先我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))
{}

推荐阅读