Where do I include header files?

closed account (Ey80oG1T)
So I have a .cpp file:

1
2
3
4
5
6
7
#include <algorithm>

class ExampleClass
{
public:
  ExampleClass();
};


And a .h file:

1
2
3
4
5
6
7
8
#pragma once

#include <algorithm>

ExampleClass::ExampleClass()
{
  sort(list.begin(), list.end());
}


So in which file do I use `include <algorithm>`. Do I include it in the .cpp file, the .h file, or both?
You have your header file and source file mixed up. The declaration of the class should go in the header file, and the constructor definition should go in the cpp file.

In this case, you should only include algorithm in the file which contains the definition of std::sort (which should be the cpp file, but you mistakenly made it the header file).
Note also that if you have a .cpp file:
1
2
3
4
5
6
#include <algorithm>

ExampleClass::ExampleClass()
{
  sort(list.begin(), list.end());
}

then compiler will report an error when it attempts to compile this .cpp file:
error: What is "ExampleClass"? What is "list"?


You must include the definition of the class:
1
2
3
4
5
6
7
#include <algorithm>
#include "exampleclass.h"

ExampleClass::ExampleClass()
{
  std::sort( list.begin(), list.end() );
}

The two most common approaches are to put all includes in .h files or the least access approach where what is used in the header (usually types, like string, vector, etc) goes in the .h and what is used by the code (algorithm, etc) is in the .cpp file.

Its more about style than rules. If you break the rules, the code won't compile due to missing the stuff you tried to use, and it will tell you and you can fix that easily enough. Once it compiles, how you organize them is irrelevant to the compiler but I strongly advise one of the 2 approaches above. You can also #include anywhere, not just top of file, but here again, I caution you on doing weirdness -- no one does this by common agreement that it is harder to follow /read / fix the code.
I normally recommend you #include all necessary #include files in all of the files that require those #includes.

header.h
1
2
3
4
5
6
7
8
9
10
11
#include <string>

class My_class
{
    public:
        void print();

    private:
        std::string name;
};


mine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
#include <header.h>

int main()
{
     My_class mine;
     mine.print();

     std::string test ("Hello World\n\n");

     std::cout << test;

}


The <string> header is #included in both files because the #inclusion is required in both files. Don't rely on some other file to #include a required header as this could lead to "mysterious" compile failures when the order of the #includes change.

Don't rely on some other file to #include a required header as this could lead to "mysterious" compile failures when the order of the #includes change.

This. You should include a header file in every place that needs to have it.

The exception would be that if the header defining a class requires the inclusion of another header file, then there's no need for the source file for that class to include it too.

So, for example, if A.h defines a class, and A.cpp contains the implementation of that class, then you know that A.cpp will ALWAYS include A.h. So if A.h and A.cpp both need to have class B defined, and A.h includes B.h, there's no need to make A.cpp include B.h too.

Last edited on
The exception would be that if the header defining a class requires the inclusion of another header file, then there's no need for the source file for that class to include it too.


what does a class have to do with it?
if you have a .cpp file that needs a .h file (even if its procedural code, with function headers in the .h) you only need the include in the .h as the cpp needs the .h and inherits.

what does a class have to do with it?

I'm highlighting a specific case where you can make some safe assumptions about which header file will always be included in the source file. You can always assume that A.h will be included in A.cpp, because it would be nonsense to have a source file inplementing the methods of a class without including the header that defines the class.

Since we know that A.cpp will always include A.h, then we can be sure that, if A.h includes B.h, there's never any need to have A.cpp explicitly include B.h. It's one of the few times we can relax the rule that you should always include the header file in every file that needs it, because we can be certain that A.cpp will never stop including A.h.
Topic archived. No new replies allowed.