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
 
Ad

Advertisements

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

Advertisements

Ad

Advertisements


Top