Problem with #include w/ complicated file structure

So I'm working on a little computer game doodad, I'm a relative newbie at c++ so there's likely a solution to my problem using some sort of syntax I don't know.

I was planning on using a separate file for each major class. Here's the "structure" of the program, as in each lower level will use functions defined in the classes above it.

displayText, displayImage, mouseInterface

tooltip, rightclickmenu, staticmenu, scrollbar

The problem arises when everything is linked together with #include. Here's some pseudocode.
1
2
3
//main.h
#include "tooltip.h"
#include "staticmenu.h" 


1
2
3
//tooltip.h
#include "displayImage.h"
#include "displayText.h" 


1
2
3
//staticmenu.h
#include "displayImage.h"
#include "mouseInterface.h" 


So the problem is that when everything is compiled, there are multiple definitions of the classes in displayImage.h because it was #included in multiple files. I figured extern would fix this, but it turns out it doesn't work on classes for some reason beyond my comprehension. Is there some way to get this to work without converting my classes to functions and extern'ing the functions? After this I really feel like classes are pretty useless and can easily be replaced with functions. Are classes just used for.... "classyness"?

First, whether you use classes or functions is irrelevant to your problem. Even if you don't use classes (as encapsulated classes with methods) you may still want to use structures. In fact, you arguably will at some point.

The idea is this - you've got type definitions and object definitions. Objects are functions and variables. I assume that you are acquainted with those. Objects can be put in separate files and compiled there and later linked to their points of reference during the linking stage. Since in the header files you only need to declare them (not define them), multiple inclusions cause no trouble. This is because several declarations do no harm. Several definitions do.

Structures and classes (whether OO or procedural style) on the other hand need to be defined in every translation unit (corresponds to preprocessed .cpp file) that uses them. Exception is made for the translation units that exclusively (and solely) use those types in pointers and references. Then declarations without definitions are acceptable. It is called forward declaration of the structure or class. But otherwise you need to provide definition, and only one definition of a structure or class. Multiple definitions due to multiple inclusion result in error.

So, how do you protect against multiple inclusion. You use the following preprocessor idiom:
1
2
3
4
#ifndef SOME_ARBITRARY_NAME
#define SOME_ARBITRARY_NAME
//header stuff goes here
#endif 

Usually SOME_ARBITRARY_NAME is derived from the header name somehow, like HEADER_NAME_H.

Regarding the use of OO. First, OO is very good thing, but if someone else does it for you. It is unbelievably (almost insanely) difficult. Consequently, it is sometimes counter-productive for small short-term projects, especially if you have no previous experience with the problem domain. But if someone asks me, "should I give you generic OO library or a C-style one," I would virtually always choose the former. When you acquire experience, your OO designs become better, more elegant, more polished, much more reusable, and you will start to like it more. But if you use it as programming with classes, as I suspect you do (no offense intended), than there is no advantage, except the exercise. Which is arguably desirable on its own.

Regards
Last edited on
wow, thanks for the informative answer!
Topic archived. No new replies allowed.