473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Nov 27 '05 #1
5 1643
* 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?
Nov 27 '05 #2
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

Nov 27 '05 #3
* 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


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?
Nov 27 '05 #4
Alf P. Steinbach wrote:
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 .

Nov 27 '05 #5
> 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/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

Nov 27 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mescaline | last post by:
Hi, Suppose a_file.cpp contains a function a_function() Now to include it in main_file.cpp I just do #include "a_file.cpp" and I'm all set. i recently came across this seemingly roundabout...
28
by: Ramesh | last post by:
Hi, I am currently maintaining a legacy code with a very very large code base. I am facing problems with C/C++ files having a lot of un-necessary #includes. On an average every C/C++ file has...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
44
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
14
by: Pedro Graca | last post by:
Imagine I have a structure with a size_t member: /* foo.h */ struct foo { char const *bar; size_t barlen; }; void make_foo(struct foo *p);
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
8
by: The Cool Giraffe | last post by:
One thing i do know for sure. When one creates a CPP file, one needs to include the H file. Now, having said that, i wonder if there are some general hints, requirements or standard guide lines on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.