Skip to main content
gamedev.net
🔒 Locked

static

Started by terryleung Jan 22, 2005 at 10:04 AM 12 replies 1.5k views
Original Post
terryleung
terryleung
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??
mike25025
mike25025
you need these two lines in a cpp file

string CConfigFile::strLevel;
float CConfigFile::gamma;
terryleung
terryleung
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
Promit
Promit
You must put the aforementioned 2 lines in a cpp file somewhere.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
mike25025
mike25025
you need to put those lines somewhere so the linker knows where they are in memory
terryleung
terryleung
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;
mike25025
mike25025
no

create a new cpp file (ConfigFile.cpp)
and put these lines into it

#include "ConfigFile.h"string CConfigFile::strLevel;float CConfigFile::gamma;
terryleung
terryleung
thx all

i made it
Miserable
Miserable
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.
terryleung
terryleung
because i only have one config file
and i want to make it easily to be used in all the classes
Miserable
Miserable
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.)
terryleung
terryleung
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?
Miserable
Miserable
You can probably Google for it, but if you really want to know about singletons and other design patterns, consider purchasing the "Gang of Four book": Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson and Vlissides.
Promit
Promit
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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.