Bare with me :/
I have two class definitions in file MyClass.h:
I then have a file named globals.h with a global object of type MyClass in a namespace:Code:#define MYCLASS_H class MyClass { int i; public: MyClass(int n):i(n){} int GetI(void){return i;} }; class MyClass2 { float x; public: MyClass2(float n):x(n){} float GetX(void){return x;} void DoStuffToX(void); };
I then have a file called MyMain.cpp which includes both header files and refers to my global MyClass object:Code:#define GLOBALS_H #ifndef MYCLASS_H #include "myClass.h" #endif namespace my_name { MyClass myObject(234); }
Code:#include <iostream> #ifndef GLOBALS_H #include "globals.h" #endif #ifndef MYCLASS_H #include "myClass.h" #endif int main() { cout << my_name::myObject.GetI(); cin.get(); return 0; }
Now, at this point in the operation everything compiles and works fine; we have an output on the screen of "234". Hoorah. The error occurrs when I try adding another file wanting to include globals.h. This one is called MyClass2.cpp:
I try to compile this (all four files) and I get the following linker errors:Code:#ifndef MYCLASS_H #include "myClass.h" #endif #ifndef GLOBALS_H #include "globals.h" #endif void MyClass2::DoStuffToX() { x *= static_cast<float>(my_name::myObject.GetI()); }
This is of course a simplified version of a much more complicated program, where I would really like to be able to have a global variable that can be included by more than one file. Any light on this would be much appreciated.d:/dom/my documents/developing tools/temp programming/globalobjects/myclass2.o: In function `MyClass2:oStuffToX(void)':
//d/dom/my documents/developing tools/temp programming/globalobjects/myclass2.cpp:9: multiple definition of `my_name::myObject'
d:/dom/my documents/developing tools/temp programming/globalobjects/mymain.o(.bss+0x0)://D/DEV-C_~1/Include/G__~1/streambuf.h: first defined here
dt



LinkBack URL
About LinkBacks
oStuffToX(void)':



I used to be an adventurer like you... then I took an arrow to the knee.