[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

.H files AND .CPP files -- How should I organize my fairly complex program?

Posted on 2004-11-15
5
Medium Priority
?
311 Views
Last Modified: 2010-04-01
Currently... I have ~25 classes. They all share the same header file... I know! This is bad!

I think I need to have several folders with related classes in these folders... with each related class sharing a header file...

So instead of...

1-Large-Header-File
25-Classes which "include that one header file"

I need...

1 Master Header File
5+ Sub-Header-Files
Then the .cpp files



One problem..

Let's say I have this setup:

File: MainHeader.h
#include "A.h"
#include "B.h"

File: A.h
class Alpha
{...
list<Bingo*> BingoList;
...};

File: B.h
class Bingo
{...
list<Alpha*> Alpha;
...};

File: Alpha.cpp
#include "MainHeader.h"
.... "uses the Bingolist declared in A.h"

#include "MainHeader.h"
File: Bingo.cpp
.... "uses the Alpha list declared in B.h"


There will be an error -- Class Alpha will not recognize Bingo & Bingo class will not recognize Alpha ...

Or atleast one of them won't... I think they both won't.

What is the proper way of getting around this problem and having an organized setup of my classes?

Thanks!
0
Comment
Question by:oxygen_728
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
georg74 earned 1400 total points
ID: 12590044
hi,

if you really need mutual class usage, then
the proper way is "forward declaration":

File: A.h
----------
class Bingo; // forward declaration

class Alpha
{...
list<Bingo*> BingoList;
...};

File: B.h
----------
class Alpha; // forward declaration

class Bingo
{...
list<Alpha*> Alpha;
...};

----------------

depending on the real problem, there are other solutions:
- define a common base class and use polymorphismus (virtual functions)
- use templates

gl,
georg
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 600 total points
ID: 12590115
I recommend you make 25 header files, and name each header file using the same name of the class.

One of the nice features of Java, is that each class has it's own file.
This makes it much easier to find the code you need, and there no guess work to figure out what header file you need for a paticular class.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 600 total points
ID: 12590128
To add to georg74's comment, in your *.cpp file is where you'll want to include both headers.

File: A.cpp

#include "A.h"
#include "B.h"

Same thing with the B.cpp file.
0
 

Author Comment

by:oxygen_728
ID: 12590585
Ok.. I did some experimenting around.

Just for clarification for people who visit this post with the same question I had..

NOTE: Make sure you do a "using namespace std;" in your main header file... AFTER the windows "includes" but BEFORE your sub-header includes.

This setup works properly for Templates, Inheritence, and mutual class usage:

MainHeader.h (#includes on everything)

Subheader1.h (#include MainHeader.h)
SubHeader2.h (#include MainHeader.h)

If you need to use one of the classes prototyped within SubHeader1 within one of the classes within SubHeader2... then the top of SubHeader2.h should look like this:

#include "MainHeader.h"
class ClssFrmSub2;

class SubHdr1Class
{
  ClssFrmSub2 PtrThatWorks;
  ...
}

Don't forget to put the "using namespace std;" in the right place -- If you don't it will mess up your inheritence and your Templates!



To the guys who helped me - Thanks! I thought I was on the right track with Forward Declaration... but it turns out I goofed with the "using namespace std;" thing... The errors were very vague OR I didn't read into them well enough.

Thanks for the help!

Brian
0
 

Author Comment

by:oxygen_728
ID: 12590589
btw.. gonna do 350 for george, 150 for Axter .... I hope that's fair for the Main Answer and Support Answers.

Thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question