I'm more into JAVA, but when HI-Perf is in the requirements list C/C++ must come on the table. And so it did. And, as expected, I'd stumbled upon something I cannot understand and cannot dig out in SO. So can anyone explain me why didn't guardian in a header below work?
// a0a.h
#ifndef _A0A
#define _A0A
class State {
public:
static State* A;
}
State* State::A = new State(); /* this is going to be troublesome */
#endif
.
B.h // #include "a0a.h"
C.h // #include "a0a.h"
XXX.h // #include "B.h"
XXX.h // #include "C.h"
and compilation gives me a multiple definitions error on the line pointed out above.
I've made it work by moving that line away to a .cpp source file, but I still want to understand why didn't guardian protect me from shooting my own foot here?
I hope I do not have to tell that this is just a dummy code reflecting what I did -- not the actual code I was writing.
EDIT
Repeated Multiple Definition Errors from including same header in multiple cpps does not explain WHY. I mean I do not get the picture from any of those answers -- they just give suggestions (none of which I need).
Of course I can dive into books and probably learn how all of this makes compiler sneeze in my console, but asking is SO is much faster and will most likely help someone else to understand.
State::A
is included. If you included multiple times, you will have multiple definitions forState::A
, which is not legal: the linker will issue errors. To avoid this, defineState::A
in an implementation file (*.cpp).