Including headers: base class and derived class

Hi,

I am working on OOD in C++ and have just started to learn about classes and inheritance. So far I could follow the tutorial and my programs have compiled and have executed as expected.

I have 2 classes. Class baseClass and class derivedClass that is a public inheritance from baseClass. Previously I had both class declarations in the same header file (classes.h) and both implementations in the same cpp file (implementation.cpp). The program compiled, no problems.

Now I want to split the code, i.e. each class should have its own header and its own implementation.cpp. My question is, how do I handle all this with the header includes, as I keep getting errors that my base class gets "redefined". My research so far implies that I should use

1
2
3
4
#ifndef ????
#define ????

#endif 


to prevent that headers get included twice.

Can someone please explain me how to include and link these files properly?

Here's my current settings. All files are part of the same project. In Dev-cpp I don't need to link the implementation files anywhere, as the compiler does so automatically, at least it had worked previously when I had only 1 class (1 header + 1 implementation.cpp + main.cpp).

baseclass.h
- no includes
- contains class declarations

baseclass.cpp
- #include "baseclass.h"
- contains base class implementation

derivedclass.h
- #include "baseclass.h"
- contains class derived: public base

derivedclass.cpp
- #include "derivedclass.h"
- contains derived class implementation

main.cpp
- #include "baseclass.h"
- #include "derivedclass.h"
- contains main()

Again, all I did is separate the base and derived class into saperate header/implementation files and it stopped working. In my tutorial the section following is very briefly talking about how to avoid including a header twice (which seems to be the problem), but I don't quite understand how to do this, and where to put the code section quoted above.

Thanks a lot in advance. Help is greatly appreciated! :)
Last edited on
OK,

so where (in what file) do I put the include guard? I am not sure in what order the compiler assembles the code, i.e. at what point an #include is inserted for the 2nd time...

When I delete #include "baseclass.h" from the "derivedclass.h" the program compiles, so I suppose there?

Yet when I include
1
2
3
4
#ifndef PROTECT_MAKRO
#define PROTECT_MAKRO
#include "baseclass.h"
#endif 


in the derivedclass.h the program doesn't compile. Could you advise me how to use the include guard?
Last edited on
so where (in what file) do I put the include guard? I am not sure in what order the compiler assembles the code, i.e. at what point an #include is inserted for the 2nd time...

You put it in every single header file you write. You can't always predict the order in which you, or other people, will want to include your headers in the future, so put the protection in every single one.

Many IDE's will do it for you automatically when you first create a header file.

(Compilers start at the top of the translation unit and work down, so the order is completely deterministic. Nevertheless, that's no reason to omit inclusion guards, even if you think you have a header file that won't need them for the particular project you're working on at the moment.)

From the code snippet you've posted, it looks as though you haven't read the info about include guards carefully enough. You don't put them around the include statement - you put them around the contents of the header file.
Last edited on
Yes, that makes sense. I just didn't quite understand the wikipedia article. ;)

I got it now using #pragma once in every header and the program compiles.
Thanks a lot!
Last edited on
You're welcome. Yes, #pragma once is another way of doing it, if you're working with a compiler that supports it.
Topic archived. No new replies allowed.