Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK2001 error when accessing static variables C++

Tags:

c++

static

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 external symbol "private: static unsigned int Platform::tex_plat" (?tex_plat@Platform@@0IA)

I have initialised the variable properly in the cpp file, however I believe this error occurs when trying to access it in another method.

.h

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;
};

.cpp classes: This is where the variable is initialised

int Platform::loadTexture(){
 GLuint tex_plat = SOIL_load_OGL_texture(
            "platform.png",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_INVERT_Y
            );

    if( tex_plat > 0 )
    {
        glEnable( GL_TEXTURE_2D );
        return tex_plat;
    }
    else{
        return 0;
    }
}

I then wish to use the tex_plat value in this method:

void Platform::draw(){
    glBindTexture( GL_TEXTURE_2D, tex_plat );
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_POLYGON);
    glVertex2f(xCoord,yCoord+1);//top left
    glVertex2f(xCoord+width,yCoord+1);//top right
    glVertex2f(xCoord+width,yCoord);//bottom right
    glVertex2f(xCoord,yCoord);//bottom left
    glEnd();
}

could some one explain this error.

like image 285
Matt Avatar asked Apr 06 '13 00:04

Matt


1 Answers

add this:

GLuint Platform::tex_plat;

after your class declaration.

like image 123
gongzhitaao Avatar answered Oct 22 '22 03:10

gongzhitaao