🔒 Locked
static
Advertisement
Original Post
i define a configFile class like that class CConfigFile { public: CConfigFile(void); ~CConfigFile(void); static string strLevel; static float gamma; static void Load() { ifstream fin("Config.ini"); // Check if the file was not found or could not be opened if(fin.fail()) { // Display an error message and quit the program if file wasn't found MessageBox(NULL, "Could not find Config.ini file!", "Error", MB_OK); PostQuitMessage(0); return; } // Read in the name of the level that will be loaded fin >> strLevel>>strLevel; string strGamma; // Now we need to read in the gamma level for our lightmaps fin >> strGamma>>gamma; // Close the file fin.close(); } }; whenever i use the static variable gamma or strLevel, there is a link error (no syntax error ) it says that these two variale is unresolved external symbol (LNK2001) what is the reason??
there is no cpp file, only the .h file and the all the things are defined there
and i use these two variable in other place and i have put the CConfigFile:: in front of the variable name
and i use these two variable in other place and i have put the CConfigFile:: in front of the variable name
i still not understand it very well
can you guy tell me more detail??
for example, i have another class which need to use these two variable
then i need to put the following two line in the c++ file of this class?
string CConfigFile::strLevel;
float CConfigFile::gamma;
can you guy tell me more detail??
for example, i have another class which need to use these two variable
then i need to put the following two line in the c++ file of this class?
string CConfigFile::strLevel;
float CConfigFile::gamma;
thx all
i made it
i made it
To explain the phenomenon: Regular (non-static) variables can be declared inline since they are part of the "template" (conceptually, I'm not talking C++ templates here!) to create an instance. However, obviously, there is only ever one instance in the entire program of a static variable. Therefore, the header can only provide a declaration; somewhere, there must be one and only one definition of the variable: thus, in a source file.
Looking at the class, I cannot help but wonder, however, why these variables (and the function) need to be static.
Looking at the class, I cannot help but wonder, however, why these variables (and the function) need to be static.
because i only have one config file
and i want to make it easily to be used in all the classes
and i want to make it easily to be used in all the classes
So it's basically an ad-hoc singleton: You can instantiate configFile objects all over the place, but they will share state. I think it is an inelegant solution: Either make it a proper singleton, or make it a regular class and pass it around. (Personally I would go for the latter—you may want more config files later on.)
i do this because i dont want to pass the config file all around the data structure
i am quite sure that i only need one
because this is my final year project and i only have about 10 weeks left to finish it
but i am interested in proper singleton
what is it and how to make it?
i am quite sure that i only need one
because this is my final year project and i only have about 10 weeks left to finish it
but i am interested in proper singleton
what is it and how to make it?
There's nothing wrong with what he's done. You call it an 'ad hoc singleton', but he's written something similar to a Monostate Pattern. There are several differences between the monostate and singleton, primarily semantic, but the notable one is that children of monostates are implicitly monostates as well.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement