Search code examples
c++visual-studioclassstatic-classes

Static function can not be accessed or used C++


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  

Solution

  • 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