1

I hope the title is not misleading.

I am making my own Linear Algebra Math library, just for practice and better understanding of math programming. I use the glm math library for reference and help. My types for now are:

class Vector2, Vector3, Vector4

all classes represent float vectors (later will be templated).

Vector2.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 is later defined.This piece of code is giving me use of undefined type 'Vector3'. As far as I can understand glm is doing the same thing and everything looks fine (glm is not including vec3 anywhere inside vec2). Here is a useful link that helped me understand better what is going on and it looks like it says the same thing, separate declaration/definition etc.

I did an extended search using VS Code Maps on glm's includes and dependencies on vec2 & vec3 and I couldn't find anything. What am I missing?

EDIT: My main concern is how glm is doing what my code is trying to do. I already know the "easy/right" way but I want to understand glm' s code.

I am using c++11+

Segmentation
  • 403
  • 7
  • 20
  • **glm** does not use forward declarations. – 273K Apr 14 '20 at 02:04
  • 1
    Does this answer your question? [Use of undefined type](https://stackoverflow.com/questions/18284678/use-of-undefined-type) – 273K Apr 14 '20 at 02:10
  • @S.M. Can you elaborate a little more on that? I can't understand how they can use `vec3` inside `vec2` without including or forward declaring something. As for the second comment, I already know how to do that and it's the logical thing to do, but I'm trying to understand `glm`'s way. I can edit my question if I didn't make it clear enough. – Segmentation Apr 14 '20 at 06:32
  • Please be more specific, where do you discover vec3 used in vec2. Please refer to the official code. – 273K Apr 14 '20 at 06:53
  • I seem got it. You confused with templates. vec2 does not use vec3, but vec<2, ...> uses vec<3, ...> – 273K Apr 14 '20 at 07:05
  • Is this relevant to what is going on? https://stackoverflow.com/questions/18674387/why-can-i-call-function-templates-without-forward-declarations – Segmentation Apr 14 '20 at 07:10
  • No. Two templates are used there. Only one template `vec` in glm is used. – 273K Apr 14 '20 at 07:13
  • Alright, I'll dig more into it. – Segmentation Apr 14 '20 at 07:15

1 Answers1

1

As far as I searched and understood glm uses forward declarations. type_vec.hpp which is included in every type_vecX.hpp file has declarations and typedefs for all vectors (float-bool,high-low precision) line 103.

What did the trick was the use of templates. First of all I templated the structs

template<typename T>
struct Vector2
{
public:
...
};

and for the constructor in question the changes were

Declaration:

template<typename S>
Vector2(const Vector3<S> & inVec3);

Definition

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))
{}
Segmentation
  • 403
  • 7
  • 20