![]() |
| | #1 |
| Registered User Join Date: Nov 2001
Posts: 162
| Thanks! BTW, I am using MSVC++ if that matters. Last edited by Crossbow; 06-16-2002 at 10:08 PM. |
| Crossbow is offline | |
| | #2 |
| Guest
Posts: n/a
| in the top of the class you are including use this: Code: #ifndef __H_HEADER_NAME #define __H_HEADER_NAME Code: #endif |
|
| | #3 |
| Registered User Join Date: Apr 2002
Posts: 1,571
| Do you want to use the same class instance in 2 source files? Or two different objects , in two source files...
__________________ "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers |
| MrWizard is offline | |
| | #4 |
| Registered User Join Date: Nov 2001
Posts: 162
| I want to use the same class instance in two different source files. |
| Crossbow is offline | |
| | #5 | |
| Registered User Join Date: Apr 2002
Posts: 1,571
| Quote:
| |
| MrWizard is offline | |
| | #6 |
| Registered User Join Date: Nov 2001
Posts: 162
| I did define it as extern, and then used the class in the two source files, but that game a linking error when I tried to link it. Here is the full compile output: --------------------Configuration: Normals - Win32 Debug-------------------- Compiling... 3DMath.cpp Init.cpp Main.cpp Linking... Init.obj : error LNK2005: "public: __thiscall Log::Log(char *,bool)" (??0Log@@QAE@PAD_N@Z) already defined in 3DMath.obj Init.obj : error LNK2005: "public: __thiscall Log::~Log(void)" (??1Log@@QAE@XZ) already defined in 3DMath.obj Init.obj : error LNK2005: "public: void __cdecl Log::Logout(char *,...)" (?Logout@Log@@QAAXPADZZ) already defined in 3DMath.obj Init.obj : error LNK2005: "private: void __thiscall Log::Truncate(void)" (?Truncate@Log@@AAEXXZ) already defined in 3DMath.obj Init.obj : error LNK2005: "public: void __thiscall Log::FileName(char *,bool)" (?FileName@Log@@QAEXPAD_N@Z) already defined in 3DMath.obj Init.obj : error LNK2005: "class Log Logfile" (?Logfile@@3VLog@@A) already defined in 3DMath.obj Main.obj : error LNK2005: "public: __thiscall Log::Log(char *,bool)" (??0Log@@QAE@PAD_N@Z) already defined in 3DMath.obj Main.obj : error LNK2005: "public: __thiscall Log::~Log(void)" (??1Log@@QAE@XZ) already defined in 3DMath.obj Main.obj : error LNK2005: "public: void __cdecl Log::Logout(char *,...)" (?Logout@Log@@QAAXPADZZ) already defined in 3DMath.obj Main.obj : error LNK2005: "private: void __thiscall Log::Truncate(void)" (?Truncate@Log@@AAEXXZ) already defined in 3DMath.obj Main.obj : error LNK2005: "public: void __thiscall Log::FileName(char *,bool)" (?FileName@Log@@QAEXPAD_N@Z) already defined in 3DMath.obj Main.obj : error LNK2005: "class Log Logfile" (?Logfile@@3VLog@@A) already defined in 3DMath.obj Debug/Normals.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. Normals.exe - 13 error(s), 0 warning(s) |
| Crossbow is offline | |
| | #7 |
| Registered User Join Date: Apr 2002
Posts: 1,571
| Okay....i'll give you an example. Say you have a main.cpp, a myclass.h, and a second.cpp . .. You define your class in main right? but you want to use it in second? So in your myclass.h you would do this Code: ... ... ... #ifdef MAIN_CPP #define GLOBAL #else #define GLOBAL extern #endif GLOBAL MyClass myvar; This should work. Any questions let me know
__________________ "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers |
| MrWizard is offline | |
| | #8 |
| Registered User Join Date: Nov 2001
Posts: 162
| I didn't get that to work, but I have attached a simple project with just the class and two functions in different files so you can get it to work and save the changes and post it back up. I wasn't sure whether to use the GLOBAL thing before the prototype in main.cpp or not. Thanks for your time, man. |
| Crossbow is offline | |
| | #9 |
| Registered User Join Date: Nov 2001
Posts: 162
| Here's the file: |
| Crossbow is offline | |
| | #10 | |
| Registered User Join Date: Apr 2002
Posts: 1,571
| Quote:
Code: #include <log.h>
#ifdef MAIN_CPP
#define GLOBAL
#else
#define GLOBAL extern
#endif
GLOBAL Log Logfile("log.log", true);
void function();
Code: #ifndef LOG_H // Keep going if not defined #define LOG_H // define the constant #include <log.h> #ifdef MAIN_CPP Log Logfile( "log.log", true ); #else extern Log Logfile; #endif void function(); #endif // End of statement EDIT: Made a mistake. Whenever you have int's and stuff you can do it with the GLOBAL thing like I did...but with Classes that have constructors...whenever you want it global you have to pass "log.log", true... but when you say you want it extern, you don't say "log.log", true again, you just say Log Logfile; Hope that makes sense....Also, don't declare this in your main.cpp, your header file is already declaring it!!!! Last edited by MrWizard; 06-18-2002 at 07:56 PM. | |
| MrWizard is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mutex across multiple source files | Quasar | Linux Programming | 7 | 12-04-2007 08:25 AM |
| pseudocode for multiple source files | Calef13 | C++ Programming | 4 | 11-13-2007 09:07 AM |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Need help with input streams from multiple source files | orikon | C++ Programming | 2 | 10-08-2005 02:56 PM |
| Headers with Class Objects? error! with multiple files | Dae | C++ Programming | 4 | 06-18-2005 02:26 PM |