LNK2001 error when accessing static variables C++

后端 未结 2 1034
死守一世寂寞
死守一世寂寞 2021-02-14 04:26

I\'m trying to use a static variable in my code when trying to use textures, however I keep getting this error:

1>Platform.obj : error LNK2001: unresolved ext         


        
相关标签:
2条回答
  • 2021-02-14 05:04

    add this:

    GLuint Platform::tex_plat;
    

    after your class declaration.

    0 讨论(0)
  • 2021-02-14 05:19

    Static member must be defined outside class body, so you have to add the definition and provide initializer there:

    class Platform :
    public Object
    {
        public:
            Platform(void);
            ~Platform(void);
            Platform(GLfloat xCoordIn, GLfloat yCoordIn, GLfloat widthIn);
            void draw();
            static int loadTexture();
    
        private:
            static GLuint tex_plat;
    };
    
    // in your source file
    GLuint Platform::tex_plat=0; //initialization
    

    It is also possible to initialize it inside your class but:

    To use that in-class initialization syntax, the constant must be a static const of integral or enumeration type initialized by a constant expression.

    0 讨论(0)
提交回复
热议问题