I thought that defining them, (#define, #endif) prevented the errors of multiple inclusions of the same header, so this is false? |
You misunderstand.
Include guards (#ifndef, #define, #endif), prevent the same header file from being #included multiple times in a SINGLE source file. IE if you do this:
1 2 3
|
// a.cpp
#include "a.h"
#include "a.h"
|
The second #include will effectively be skipped because of the guard.
The program is totally functional as is, |
Only because you only have one source file that includes this header. As soon as you have another source file #include that header, you'll have linker errors.
The problem is you're defining (what appears to be) multiple bodies for the same function.
#include is kind of like a copy/paste operation. The #include line is "replaced" with the contents of the file being included. So when you #include a header, it's basically putting that header at the top of the cpp file.
NOW... when a header contains a function body, this means it puts that function body in
every cpp file that includes the header which means that your program has multiple bodies for the same function. Even though they all contain identical code, the linker will see them all as different functions.
So when you try to call the function... how can the linker know which version you're calling?
Only templates, inline, or globally static function bodies should be in header files.
And the reference operator makes accessing private variables a possibility from what I understand...is there a way to avoid this as in make them completely inaccessible from outside of the class? |
Yeah.
Just make your member private and don't make a function that returns a reference to it.