Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-25-2004, 03:47 PM   #1
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
Exclamation Another C++ question

I am now so confused about source files and header files....

I have object A, defined in "A.cpp"

I have object B, defined in "B.cpp". Object B needs to know about object A to work. So, along with the header files at the top of "B.cpp", I have the text,
#include "A.cpp"

Next, I have object C, defined in "C.cpp" Object C needs to know about both A and B to work. So, along with its header includes, I have the text,
#include "A.cpp"
#include "B.cpp"

Now I know the reason for this not working, its because the includes have made the program define object A twice, and it doesn't like the redefinition. How do I get around this? The only reason I've put all of these objects in different files is for neatness. Thanks again for the help!
__________________
Desktop
| Antec Performance Plus 1080AMG | Antec 430W PSU | Intel D875PBZ | Intel P4 3.0C |
| 4x512 MB dual-channel DDR400 Kingston HyperX | ATI Radeon 9800 pro 256 MB | Creative Sound Blaster Audigy Gamer |
| LiteOn DVD-ROM | LiteOn CDRW | 36 GB WD Raptor | 250 GB WD SATA2 | Sony Floppy | XP pro |
Iniamyen is offline   Reply With Quote
Old 06-25-2004, 03:52 PM   #2
Member (10 bit)
 
speeddude2000's Avatar
 
Join Date: Mar 2004
Location: on the couch
Posts: 830
Send a message via AIM to speeddude2000 Send a message via MSN to speeddude2000 Send a message via Yahoo to speeddude2000
hmm...i know someone who knows this stuff. ill get back 2 ya tonite.
speeddude2000 is offline   Reply With Quote
Old 06-25-2004, 04:01 PM   #3
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
First of all, you should be including headers, not the actual code files. So separate the function declarations from the codes into header files (A.h, B.h, C.h)

In A.h, leave as is. In B.h, do something like this, replacing #include <A.h>:

Code:
#ifndef INCLUDEA
#define INCLUDEA
#include <A.h>
#endif
Do the same thing in C.h. That way, the first time it is encountered by the precompiler, it will include A.h, because INCLUDEA hasn't been defined yet. Once INCLUDEA is defined, the precompiler won't try to include A.h again.

Thus, when you compile C, it will include B.h, which will include A.h because INCLUDEA hasn't been defined yet. However, it won't include A.h again in C.
doctorgonzo is offline   Reply With Quote
Old 06-25-2004, 04:07 PM   #4
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
So, in the class definitions, the functions will be named, but not defined (prototyped)?

And then in the header files, the functions will be defined?

Thanks for the help, I think I can figure this out now.
Iniamyen is offline   Reply With Quote
Old 06-25-2004, 06:32 PM   #5
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
Unhappy Still not working

Okay, so far this is what I've done.

I've changed the function definitions inside the cpp code to the form:

classname::functionname() {
functiondefinition, return value;
}

and I've made .h files with prototypes of the form:

returntype functionname();

with a .h file for each class, prototyping all of its PUBLIC functions.

I then used your defining routine in both the cpp file that NEEDS the definition, and the cpp file whos class' functions are defined in the .h file. It still won't compile!

Thanks for your patience.
Iniamyen is offline   Reply With Quote
Old 06-25-2004, 06:53 PM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
What error are you getting from the compiler?
doctorgonzo is offline   Reply With Quote
Old 06-25-2004, 07:45 PM   #7
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
It says that my first object defined (it would be of class C in our example) is an unrecognized type.
Iniamyen is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:48 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2