-2

I have a question about c++ header files and their includes.

Consider the following scenario.

I have a following files and code inside :

A.h

class A
{
    // ...
};

A.cpp

#include "A.h"

// implementation of A

B.h

class B
{
    A object;
}

B.cpp

 #include "A.h"

 #include "B.h"

 /// implementation of B

When I try to build, the compiler gives an error in B.h, that can't recognize A, because I didn't include A.h.

The question is why compiler compiles header files separately, if they are included in some cpp files and include preprocessor is doing the copy / paste full content of header file and header file content will be compiled with cpp file, where it included.

  • Post real code, including things like header guards. –  Jan 30 '17 at 17:54
  • 2
    Please try to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. Then please show us the *exact* errors you get, by copy-pasting them (as text) into the body of your question. Lastly when you create the MCVE add comments on the lines where you have the errors. – Some programmer dude Jan 30 '17 at 17:54
  • Could not reproduce: http://coliru.stacked-crooked.com/a/6d394086093e950d Create a [mcve] – eerorika Jan 30 '17 at 17:57
  • 1
    Header files aren't generally compiled. They are expanded into compilation units where the `#include` preprocessor directives are. – IInspectable Jan 30 '17 at 17:57
  • The main question is that I cant understand why header files are being compiled as separate files, when they are included in cpp files and compiled with this cpp files? – someone Jan 30 '17 at 17:58
  • 1
    @someone and the answer is: Header files are not being compiled as separate files. – eerorika Jan 30 '17 at 17:59
  • @someone If you compile the header files as separate source files, then depending on the compiler you are creating a [*precompiled header*](https://en.wikipedia.org/wiki/Precompiled_header). Since that's apparently not what you want to, you should not compile the header files, only the source files. – Some programmer dude Jan 30 '17 at 18:14
  • Can you please tell us *why* the compiler will compile the header files separately? *How* are you building? What compiler are you using? What environment (terminal or IDE)? You have to give us more details or we won't be able to help you solve your problem (which seems to be a miss-configured project or makefile). Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jan 30 '17 at 18:16

2 Answers2

1

If B.h requires A.h, you should include A.h into B.h, because you can't assume that every time that B.h is included, A.h will be included before.

Of course, if you simply include A.h into B.h, you'll have A.h parsed twice (or more), so duplicate definitions and a bunch of errors, which is why you must also include header guards. See C++ #include guards for an explanation of header guards.

Community
  • 1
  • 1
Rémi Bonnet
  • 793
  • 5
  • 16
0

You might use header guards structure for both .h files:

#ifndef FILE_A_H
#define FILE_A_H

/*Your header*/

#endif

In addition to it, you might add a declaration of the class in your main header.

Majki
  • 46
  • 4