C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-16-2002, 10:05 PM   #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.
Crossbow is offline   Reply With Quote
Old 06-16-2002, 10:21 PM   #2
Unregistered
Guest
 
Posts: n/a
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
  Reply With Quote
Old 06-17-2002, 01:16 AM   #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   Reply With Quote
Old 06-17-2002, 06:29 PM   #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   Reply With Quote
Old 06-17-2002, 07:01 PM   #5
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Quote:
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.
MrWizard is offline   Reply With Quote
Old 06-18-2002, 02:19 PM   #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   Reply With Quote
Old 06-18-2002, 04:04 PM   #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
MrWizard is offline   Reply With Quote
Old 06-18-2002, 07:17 PM   #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   Reply With Quote
Old 06-18-2002, 07:20 PM   #9
Registered User
 
Join Date: Nov 2001
Posts: 162
Here's the file:
Attached Files
File Type: zip logprj.zip (86.5 KB, 13 views)
Crossbow is offline   Reply With Quote
Old 06-18-2002, 07:42 PM   #10
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Quote:
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.
MrWizard is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22