 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
vivekian Guest
|
Posted: Sun Nov 27, 2005 2:00 pm Post subject: How to include header files ? |
|
|
Have a program where an object A uses a pointer to object B in a.cpp
and object B uses object A in b.cpp. The declarations have been put in
a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
and b.cpp , i get 'previously defined here' and 'redifination' errors.
What is the solution to this ?
Thanks,
vivekian
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Nov 27, 2005 2:13 pm Post subject: Re: How to include header files ? |
|
|
* vivekian:
| Quote: | Have a program where an object A uses a pointer to object B in a.cpp
and object B uses object A in b.cpp. The declarations have been put in
a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
and b.cpp , i get 'previously defined here' and 'redifination' errors.
What is the solution to this ?
|
There are many solutions, not one.
FAQ item 39.11 gives a purely technical answer, perhaps the simplest, a
forward-declaration.
<url:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11>.
struct A;
struct B;
struct A { B* myB; };
struct B { A* myA; };
It's important to know about forward declarations.
But an often more clean solution is the abstract class solution:
struct AbstractA { ... };
struct AbstractB { ... };
struct A: AbstractA { AbstractB* myB; };
struct B: AbstractB { AbstractA* myA; }
This solution helps you factor out what's really needed for A and B to
do their work, i.e. it solves the design-level problem instead of just
alleviating the immediate C++ symptom of the problem.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
vivekian Guest
|
Posted: Sun Nov 27, 2005 3:34 pm Post subject: Re: How to include header files ? |
|
|
Alf P. Steinbach wrote :
Not too sure about this , but i think by including those header files
a.h and b.h in both a.cpp and b.cpp i do forward declare them . The
problem i am facing not circular dependency but rather much more of
redeclaration compile time errors i.e.
// sample code - a.cpp
#include b.h
#include a.h
/*..........*/
//sample code - b.cpp
#include a.h
#include b.h
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Nov 27, 2005 4:00 pm Post subject: Re: How to include header files ? |
|
|
* vivekian:
| Quote: | Alf P. Steinbach wrote :
There are many solutions, not one.
FAQ item 39.11 gives a purely technical answer, perhaps the simplest, a
forward-declaration.
url:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11>.
struct A;
struct B;
struct A { B* myB; };
struct B { A* myA; };
It's important to know about forward declarations.
Not too sure about this , but i think by including those header files
a.h and b.h in both a.cpp and b.cpp i do forward declare them . The
problem i am facing not circular dependency but rather much more of
redeclaration compile time errors i.e.
// sample code - a.cpp
#include b.h
#include a.h
/*..........*/
//sample code - b.cpp
#include a.h
#include b.h
|
Uh, well, then you just haven't come to the dependency problem yet.
For the redefinition problem just add proper #include guards to the
header files, like this:
#ifndef A_H
#define A_H
// ... contents of [a.h]
#endif
For more detailed information see section 6 through 10 of chapter
(whatever) 1.6 of the attempted "Correct C++ Tutorial", at
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
vivekian Guest
|
Posted: Sun Nov 27, 2005 5:02 pm Post subject: Re: How to include header files ? |
|
|
Alf P. Steinbach wrote:
| Quote: | Uh, well, then you just haven't come to the dependency problem yet.
For the redefinition problem just add proper #include guards to the
header files, like this:
#ifndef A_H
#define A_H
// ... contents of [a.h]
#endif
For more detailed information see section 6 through 10 of chapter
(whatever) 1.6 of the attempted "Correct C++ Tutorial", at
url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.
|
Alf,
Thanks. Things much more clear now .
|
|
| Back to top |
|
 |
EventHelix.com Guest
|
Posted: Sun Nov 27, 2005 5:09 pm Post subject: Re: How to include header files ? |
|
|
| Quote: | Have a program where an object A uses a pointer to object B in a.cpp
and object B uses object A in b.cpp. The declarations have been put in
a.h and b.h respectively. Now if i include a.h and b.h both in a.cpp
and b.cpp , i get 'previously defined here' and 'redifination' errors.
What is the solution to this ?
|
The following article should help:
http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm
--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|