RandomDever wrote:It's probably some ridiculously obvious well known programming practice, but keep in mind I don't have a college education.
I take personal offense to this. Even though I HAVE a college education, I learned to code and develop on my own time. That's taking credit away from me teaching myself and giving credit to my college which didn't teach me shit. A degree means nothing about your programming efficacy. I know plenty of college grads who can't code for shit and highschool dropouts who are coding gurus. Don't let me catch you saying shit like that again.
Considering the fact that you haven't bothered to post the offending header file, I take it we're all stabbing in the dark here? My turn.
bbguimaraes wrote:Code: Select all
namespace Debug {
bool debug;
}
#endif // SOME_FILE_H
// other_file.cpp
#include <some_file.h>
void f() {
Debug::debug = true;
}
That is most DEFINITELY not going to work. That's probably what is causing the error anyway.
Every .cpp file including that header file now has a duplicate declaration of the debug boolean flag, hence the linker errors.
YourNerdyJoe wrote:change
to
Code: Select all
namespace Debug {
static bool debug;
}
and it should work
If your definition of "work" is compile, link, and run, then yes, it will work.
If your definition of "work" is operate correctly, then no. What you have now done is given each .cpp its very own private (static) instance of the debug flag. This resolves the duplicate symbol, because every .cpp file now has its own debug flag, but if the intent of the global was to allow access to this flag throughout the program, you're still wrong.
One word:
EXTERN
Change
to
Code: Select all
namespace Debug {
extern bool debug;
}
within your header file.
Then put
into a .cpp file somewhere.
The first extern declaration tells each .cpp file including the header file that
there exists a variable declared elsewhere called debug. You are NOT allocating that variable, you are simply telling the compiler that it is allocated elsewhere.
Then the second declaration in the .cpp file actually
allocates the variable. When the linker links each file's extern declaration from the header file, they now all point back to the same variable allocated within the .cpp.