Quote:Original post by gpu_fem
Shame on your compiler for compiling this. const variables should be set to constants (known at compile time). THis may be causing you issues.
Not so. Consider the following code:
void example(int x){ // this is legal, but not known at compile time. const int whatever = x;}
Quote:
Also, if you are initializing your variables at file scope, you cannot guarantee that "ini" already has a correct value before you initialize you screen size variables - so it is likely returning NULL. This is a recipe for disaster.
In a single file, you have guaranteed order of initialisation. It is across files that cause issues. However, in this case the INI constructor probably initialises it to "empty", so yes there will be NULL pointers.
Quote:
I am pretty sure thats the problem, but how exactly would i call the load function in the right scope?
One hackish way is like this:
CIniFile load(const std::string &path){ CIniFile file; file.load(path); return file;}CIniFile ini = load("whatever.ini");
But I wouldn't recommend this approach. It depends on CIniFile respecting the rule of three also.
I think it is best to do this inside main. The primary reason is error handling, outside main there is very little you can do. Because the ini file is external, it needs to be treated as any other form of input and needs to be tested to ensure its correct. You can handle absent values by using defaults, or perhaps informing the user their ini file is corrupted if you cannot read the correct values. But you certainly should not crash for something like this.
Move all the code inside main, and your problems disappear [smile]. Well, not entirely. After all you might be relying on the global values of SCREEN_WIDTH and so on through a number of functions. You could either make them non-const (which is again a bit of a hack) or find other ways to communicate such information. The "best" method depends on the exact kind of information you are trying to access.