How to include glm::vec2 in a header file?

Hello,

How can we include glm::vec2 to use it in a class ?

I have:
DeformationMesh.h

#pragma once
include "glm/vec2.hpp"

class DeformationMesh
{
public:
    void setOffset( int col, int row, glm::vec2 offset ) ;
} ;

DeformationMesh.cpp

#include "DeformationMesh.h"
void DeformationMesh::setOffset( int col, int row, glm::vec2 offset )
{ }

ofApp.cpp

#include "ofApp.h"
#include "DeformationMesh.h"

void ofApp::setup()
{
    DeformationMesh deformation ;
    deformation.setOffset( 0, 0, glm::vec2() ) ;
}

The app compiles but crash:

Exception code=0xc0000005 flags=0x0 at 0x0109660C. Access violation - attempting to read data at address 0xFFFFFFFF

If I replace #include "glm/vec2.hpp" by #include "ofPoint.h" in DeformationMesh.h, the app compiles and works.

#include <glm/glm.hpp>, as adviced here, compiles and crashs.

Is it possible to use glm without importing the ofPoint ?

Thank you.

OF 0.11.0 / mingw32 / qtcreator

Not sure but as far as i can remember glm::vec2() doesnt work anymore since the last glm update. You need to give it a initial value. It works when you include ofPoint.h because i guess in there a special glm macro is defined which makes it so you dont have to give an initial values to glm objects.

Like i said, not sure and i cant test right now, but try glm::vec2(1.0) instead of glm::vec2()

Thank you Marco. I tried:

deformation.setOffset( 0, 0, glm::vec2(0.0) ) ;

and

deformation.setOffset( 0, 0, glm::vec2(0.0,0.0) ) ;

but the app still crashes.

Can you try this?

#define GLM_FORCE_CTOR_INIT
#include <glm/glm.hpp>

instead of

include "glm/vec2.hpp"

Yes @marco_v 's last reply is probably what is needed.

The safer thing to do would be to just include ofConstants.h or ofMain.h as any defines or glm includes that are needed will be set there.

#define GLM_FORCE_CTOR_INIT
#include <glm/glm.hpp>

works ! Thank you !

[edit]

#define GLM_FORCE_CTOR_INIT
#include "glm/vec2.hpp"

also works.
[/edit]

Hi Theo,

The safer thing to do would be to just include ofConstants.h or ofMain.h as any defines or glm includes that are needed will be set there.

I’m not 100% sure of this strategy, but I always try to use the minimal set of includes in my classes, to reduce the compile time.