Thread: using a class in multiple source files???

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Question using a class in multiple source files???

    I have a log file class that outputs data to a logfile from a program. Well, I have two source files in a project I want to use this log class in. I have a header that is included by both source files. It gives me some a link error saying it was already defined in one of the source files. I was using extern too. I have tried many things, but none work. How do I make it so that I can use the class in both source files?

    Thanks!

    BTW, I am using MSVC++ if that matters.
    Last edited by Crossbow; 06-16-2002 at 10:08 PM.

  2. #2
    Unregistered
    Guest
    in the top of the class you are including use this:

    Code:
    #ifndef __H_HEADER_NAME
    #define __H_HEADER_NAME
    and at the end

    Code:
    #endif
    with that you only define the header once, no matter how may times you include it

  3. #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

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    I want to use the same class instance in two different source files.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Crossbow
    I want to use the same class instance in two different source files.
    Then you want to use the extern keyword. If you don't know how to use it, just write back and i'll give an example.

  6. #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)

  7. #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;
    That would be your header. In your main.cpp *BEFORE* you include myclass.h simply #define MAIN_CPP

    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

  8. #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.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Here's the file:

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Crossbow
    Here's the file:
    I do not have the header file Log.h .. If you want to attach that I can help. Just upon looking at it I see one problem with your header file. This is YOUR code
    Code:
    #include <log.h>
    
    #ifdef MAIN_CPP
    #define GLOBAL
    #else
    #define GLOBAL extern
    #endif
    
    GLOBAL Log Logfile("log.log", true);
    
    void function();
    It should be like this....

    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
    See if that works.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 08:25 AM
  2. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 09:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  5. Replies: 4
    Last Post: 06-18-2005, 02:26 PM