Sign in to follow this  
thecoast47

static class members

Recommended Posts

thecoast47    255
Okay so im making a game and i have to manage texture references that are returned from opengl when i load textures.

[u]HOW THE MANAGER WORKS:[/u]

The manager is only has static members so i can access them anywhere.
The manager works by associating a texture reference with the a keyword generated from the path of the image.
for example,lets say i load an image from C:/blah/blah/image.png
the Manager will generate the keyword : IMAGE.PNG
If the keyword already exists, the texture has already been loaded and then it searches for the texture reference.
If the keyword doesnt exist then the manager will load the texture and assign the keyword to that texture.

[u]MY PROBLEM:[/u]
The main problem is that i when i call the static public procedures i cant interact with the static private members of the class ,
It wont compile because the compiler gives the error :"UNDEFINED REFERENCE".

Example:
[code]
#include <iostream>
#include <string>
class Manager{
public:
static void SET_PATH(std::string INIT_PATH){
PATH = INIT_PATH;
}
static std::string GET_PATH(){
return PATH;
}
private:
static std::string PATH;
};

int main(){
std::string TEST_PATH("C:/TEST");

Manager::SET_PATH(TEST_PATH);//Undifined reference to 'Manager::PATH'

return 0;
}

[/code]

Share this post


Link to post
Share on other sites
SiCrane    11839
When you create a static member variable in a class you only have a declaration for the variable. In order for your program to link you also need to add a definition for the variable. In once source file in the same namespace as the class you need to add a line that looks like:
[code]
std::string Manager::PATH;
[/code]

Share this post


Link to post
Share on other sites
thecoast47    255
Hidden
[quote name='SiCrane' timestamp='1311298035' post='4838730']
When you create a static member variable in a class you only have a declaration for the variable. In order for your program to link you also need to add a definition for the variable. In once source file in the same namespace as the class you need to add a line that looks like:
[code]
std::string Manager::PATH;
[/code]
can i define and declare in the class?
I kinda wanted all the code to be in one header so all i have to do is call the static functions and the rest is taken care of by the static sub procedures written in the header
[/quote]

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this