* 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.
--
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?