How to include header files ?

V

vivekian

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
 
A

Alf P. Steinbach

* vivekian:
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.
 
V

vivekian

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
 
A

Alf P. Steinbach

* vivekian:
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

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>.
 
V

vivekian

Alf said:
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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top