1

I am currently working on a project in C++. In short, I have 3 classes and a main.cpp file .

1) List class //uses a vector 2) Heap class 3) Tree Class //BST

To organize myself properly, I split each class up into separate files, namely a .hpp and .cpp file.

Of course to access all the functions in each class in my main.cpp file, I #included all three .hpp files.

Everything was working cohesively until I #included the “List.hpp” file in the “heap.hpp” and the “Tree.hpp”.

What’s weird is there was no error that showed up when I only #included the “List.hpp” in a single separate header file like “heap.hpp”.

But when I #included “List.hpp” into both for some reason methods that worked in the past don’t work anymore.

Only the methods which relied on “List” as a parameter don’t work anymore (they just don’t return anything).

I’m sure not why this happened. It’s the second time it’s happened to me and I’d like to know why so I don’t make the mistake int the future.

user720280
  • 11
  • 2
  • *Can I include multiple .hpp files inside another .hpp file?* --> Yes, you can. For the rest, we must see your code. – A M Apr 11 '20 at 07:52
  • Consider adding an include guard, i.e. the two lines #ifndef LIST_HPP #define LIST_HPP at the start of the file, and #endif at the end. If your list is templated, have a look [here](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – mutableVoid Apr 11 '20 at 07:58
  • Yes, this smells of a missing include guard. – bitmask Apr 12 '20 at 08:04
  • Aside from the answers already mentioning header guards, maybe post the code of your files, if they are not too big. There are many mistakes to make which aren't exactly intuitive to understand. For example, you don't want to implement functions in headers, except if they are declared `inline` (although that would result in a compile time error rather than a runtime bug). Note that especially for a beginner, it is beneficial to post your code on [CodeReview](https://codereview.stackexchange.com/) to improve your style. – Aziuth Apr 12 '20 at 09:30

3 Answers3

2

Simple rule:

All files, independent if it is a *.cpp or *.h file, should include all other header files which are required to compile this file itself.

If a *.h file needs a definition, from another *.h file, so you should include it directly from here.

If you do it not in this way, you have to deal with the correct order of including your header files from a third file, which can be frustrating.

But you will see as your projects grows, you will often have not longer used includes and even files which do not include all needed dependencies but compile fine, because other files included before are have already included what you need.

There is a helper to manage such things: include what you use

And also a basic: Use include guards like #pragma once or the good old style #ifndef XYZ #define XYZ #endif

Klaus
  • 24,205
  • 7
  • 58
  • 113
2

in your header files,use forward declaration instead of including headers and include them in your source files, unless you have strong reason to include them.

MH Alikhani
  • 110
  • 7
0

First of all, the extension does not matter. An include file can be hpp, h or anything else, even cpp is possible. The preprocessor looks at the #include directive and what file you indicated. Still though h and hpp are the conventions to use, so please don't include cpp files.

Now, in order to understand how the compiler works, every #include directive means that the content behind that file is fully processed. It could equivalent to pasting the contents of the whole file into your c/cpp file.

Now imagine how much the compiler for that file will be processing, especially if you have #includes in your other includes.

Using forward declarations is a practice you find in most of the includes and should be preferred if want to avoid code collisions. Having said that, there still exist situations, where you want to avoid forward declarations, because if the code segment will be built up through libraries or other objects you link, you might loose performance. But this happens normally only when you very simple function called very often, opportunities where compiler would be able to inline your functions -> Optimizations.

Just keep that in mind.

Gerhard Stein
  • 1,543
  • 13
  • 25
  • "The compiler looks at ". No :-) This is the preprocessor which is doing that. – Klaus Apr 12 '20 at 07:17
  • Fine I corrected it. Still though I execute gcc or g++ depending on the language and get that result. Of course preprocessor is one part of that executables code. – Gerhard Stein Apr 12 '20 at 08:00