LNK2001 error when accessing static variables C++

后端 未结 2 1038
死守一世寂寞
死守一世寂寞 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: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.

提交回复
热议问题