0

I am trying to use a static class, but for some reason I am getting a really not helpful error.

This is the static class I am trying to access:

static class Camera
{

public:
    Camera();
    ~Camera();

    static glm::mat4 viewMatrix;

    static void move(float x, float y, float z) {// add 3 more values for the cube
        viewMatrix = glm::lookAt(glm::vec3(x, y, z),glm::vec3(0.0f, 0.0f, 0.0f),glm::vec3(0.0f, 1.0f, 0.0f));
    } 


    static glm::mat4 getViewMatrix() {
        return viewMatrix;
    }
};

This is the way I am trying to access a function:

 Camera::move(xdist, ydist, zdist);

The error:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "public: static struct glm::detail::tmat4x4<float> Camera::viewMatrix" (?viewMatrix@Camera@@2U?$tmat4x4@M@detail@glm@@A) sample  
T.ChD
  • 41
  • 4
  • Since static members needs to be initialized once at program startup, you need to create a `.cpp` file with the following definition: `glm::mat4 Camera::viewMatrix(parameters_here);` – Gilles-Philippe Paillé Oct 24 '19 at 19:10
  • @Gilles-PhilippePaillé viewMatrix does not take any parameters. Also, this is part of the class what do you mean by creating another .cpp – T.ChD Oct 24 '19 at 19:12
  • The linked duplcated question is very broad and does not give any clue on how OP should fix his problem. A better linked question/answer should be provided. – Gilles-Philippe Paillé Oct 24 '19 at 19:14
  • Just wondering can you try to initialize the static variable to something like this in this example https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm – BhanuKiran Oct 24 '19 at 19:14
  • @Gilles-PhilippePaillé, the problem of `static` class members described in the second answer. https://stackoverflow.com/a/12574407/434551. – R Sahu Oct 24 '19 at 19:18
  • 1
    What is a static class? This is not C++. – Lightness Races in Orbit Oct 24 '19 at 20:06
  • 1
    @LightnessRacesinOrbit maybe C# or even Java. Definitely means that the question identified as duplicate is irrelevant. – Mark Ransom Oct 24 '19 at 22:29
  • In C++, a "static class" has no meaning. You have to define [static members](https://learn.microsoft.com/en-us/cpp/cpp/static-members-cpp?view=vs-2019) in your cpp file . – Jeaninez - MSFT Oct 25 '19 at 07:26

1 Answers1

0

Solved, added this to the cpp file:

 glm::mat4 Camera::viewMatrix = glm::mat4(1.0);

Needed to initialize the viewMatrix in the .cpp not in the .h file

T.ChD
  • 41
  • 4