Topic: Header File Include Problem


Author: csun@netcom.com (Charles Sun)
Date: 1995/07/15
Raw View
Thanks for all the reply. I got it working.

Charles


Charles Sun (csun@netcom.com) wrote:

: Hi,

: I have a question about handling the inclusion of *.H files.

: MY SITUATION:
: To make my c++ program modular, I place the code for each class
: into two files: *.h (for class definition) & *.cpp (for member function).

: I have a based class A & two of its derived class A1 & A2.
: Therefore, in both A1.h & A2.h, there is #include "A.h" to reference
: the base class A.

:           A.h
:          / \
:       A1.h  A2.h

: Now, if my program MAIN.CPP needs both class A1 & A2
: (include "A1.h" "A2.h"), I have multiple declarations of A.
: My Borland C++ compiler returns an error message.

:           A.h
:          / \
:       A1.h  A2.h
:          \ /
:          MAIN.CPP

: MY QUESTION:
: What is a clean & nice way to get around this multiple definition
: problem (while keeping my modular files) ?
: Shouldn't the compiler/preprocessor be smart enough to handle this
: (recognizing the repeated inclusion of same .H file) ?

: Any comment or suggestion is greatly appreciated.
: In addition to posting the solution, please CC your reply to
: my email address. Thanks.

: Charles Sun
: csun@netcom.com








Author: devitto@london.sinet.slb.com (Dom De Vitto)
Date: 1995/07/11
Raw View
Charles Sun (csun@netcom.com) wrote:

> Hi,

> I have a question about handling the inclusion of *.H files.

> MY SITUATION:
> To make my c++ program modular, I place the code for each class
> into two files: *.h (for class definition) & *.cpp (for member function).

Out situation: We don't care.

This group is for the discussion of the developing ANSI C++ standard.

Dom





Author: jskim@rose.etri.re.kr (Kim Jungsun)
Date: 1995/07/08
Raw View
In article <csunDBD5sJ.K2y@netcom.com>, csun@netcom.com (Charles Sun) writes:
|>
|> Hi,
|>
|> I have a question about handling the inclusion of *.H files.
|>
|> MY SITUATION:
|> To make my c++ program modular, I place the code for each class
|> into two files: *.h (for class definition) & *.cpp (for member function).
|>
|> I have a based class A & two of its derived class A1 & A2.
|> Therefore, in both A1.h & A2.h, there is #include "A.h" to reference
|> the base class A.
|>
|>           A.h
|>          / \
|>       A1.h  A2.h
|>
|> Now, if my program MAIN.CPP needs both class A1 & A2
|> (include "A1.h" "A2.h"), I have multiple declarations of A.
|> My Borland C++ compiler returns an error message.
|>
|>           A.h
|>          / \
|>       A1.h  A2.h
|>          \ /
|>          MAIN.CPP
|>
|> MY QUESTION:
|> What is a clean & nice way to get around this multiple definition
|> problem (while keeping my modular files) ?
|> Shouldn't the compiler/preprocessor be smart enough to handle this
|> (recognizing the repeated inclusion of same .H file) ?
|>

 Compiler needs your help to exclude the possibility of multiple
inclusion. You have to protect each of your header files using #ifdef.

// In File A.h
#ifndef A_H
#define A_H

...

#endif /* A_H */

// In File A1.h
#ifndef A1_H
#define A1_H
...
#endif /* A1_H */

Do the same protection in A2.h.  Hope this help.

-- Jungsun Kim





Author: csun@netcom.com (Charles Sun)
Date: 1995/07/07
Raw View
Hi,

I have a question about handling the inclusion of *.H files.

MY SITUATION:
To make my c++ program modular, I place the code for each class
into two files: *.h (for class definition) & *.cpp (for member function).

I have a based class A & two of its derived class A1 & A2.
Therefore, in both A1.h & A2.h, there is #include "A.h" to reference
the base class A.

          A.h
         / \
      A1.h  A2.h

Now, if my program MAIN.CPP needs both class A1 & A2
(include "A1.h" "A2.h"), I have multiple declarations of A.
My Borland C++ compiler returns an error message.

          A.h
         / \
      A1.h  A2.h
         \ /
         MAIN.CPP

MY QUESTION:
What is a clean & nice way to get around this multiple definition
problem (while keeping my modular files) ?
Shouldn't the compiler/preprocessor be smart enough to handle this
(recognizing the repeated inclusion of same .H file) ?

Any comment or suggestion is greatly appreciated.
In addition to posting the solution, please CC your reply to
my email address. Thanks.

Charles Sun
csun@netcom.com