Multiple Definition problems
Hi guys,
I wonder if you can help me out and point me to the area of my brain that is defective.
Basically what I have is the following:
Code:
//main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include "extra.h"
#endif
Code:
//main.cpp
#include "main.h"
int main() {
ext_implement = new Extra();
return 0;
}
Code:
//extra.h
#ifndef _EXTRA_H_
#define _EXTRA_H_
class Extra {
public:
Extra();
void DoSomething();
private:
};
Extra *ext_implement;
#endif
Code:
//extra.cpp
#include "extra.h"
Extra::Extra() {
}
void Extra::DoSomething() {
}
Code:
//another.h
#ifndef _ANOTHER_H_
#define _ANOTHER_H_
#include "extra.h"
class Another {
public:
Another();
};
#endif
Code:
//another.cpp
#include "another.h"
Another::Another() {
ext_implement->DoSomething();
}
And here is the errors I receive:
obj\Debug\main.o: In function `main':
C:/Personal/Code/Test/main.cpp:3: multiple definition of `ext_implement'
obj\Debug\extra.o:C:/Personal/Code/Test/extra.cpp:3: first defined here
obj\Debug\another.o: In function `ZN7AnotherC2Ev':
C:/Personal/Code/Test/another.cpp:3: multiple definition of `ext_implement'
obj\Debug\extra.o:C:/Personal/Code/Test/extra.cpp:3: first defined here
I realise that I am defining ext_implement multiple times due to me having the line
#include "extra.h" (where ext_implement is defined) in a few different places, but I am
not sure of the right way to do it.
I need to have access to that variable in a few different places, so I don't mind what the solution is really. As long as there is one :)
If you need more detail please let me know.
Any pointers would be greatly appreciated.
Thanks in advance...
Shiver