C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to include header files ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
vivekian
Guest





PostPosted: Sun Nov 27, 2005 2:00 pm    Post subject: How to include header files ? Reply with 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 ?

Thanks,
vivekian

Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Nov 27, 2005 2:13 pm    Post subject: Re: How to include header files ? Reply with quote



* 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





PostPosted: Sun Nov 27, 2005 3:34 pm    Post subject: Re: How to include header files ? Reply with quote



Alf P. Steinbach wrote :
Quote:
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


Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Nov 27, 2005 4:00 pm    Post subject: Re: How to include header files ? Reply with quote

* 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





PostPosted: Sun Nov 27, 2005 5:02 pm    Post subject: Re: How to include header files ? Reply with quote

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





PostPosted: Sun Nov 27, 2005 5:09 pm    Post subject: Re: How to include header files ? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.