Why do I have to include every component header?

So, I’m following the C++ Tanks vs Zombies Live Training videos from Unreal Engine’s YT channel, and I’ve done everything right to this point (I’m currently setting up the binds for the player’s movement), but every component I use on my code I have to manually include its header file on my header. On the videos they simply type


InputComponent->BindAxis("MoveX", this, &AClassTank::MoveX);

on the code and it works fine, I get a red underline telling me that “A pointer to a incomplete class is not permitted”, and to fix that I have to go to my headers file and


#include "Components/InputComponent.h"

.

I had to include the “CameraComponent.h”, “ArrowComponent.h” and not this “InputComponent.h”.

I’ve read on the forums and done somethings I’ve found about this situation but nothing seems to help.

How can I fix this and not have to include every single component I use just like them on the stream?

First of all, for the most part you can ignore red underlines. Intellisense doesn’t know much about UE4 and it’s macros.

Secondly, in order to use or call functions on a class - the .cpp file needs to know what that class is. Including other headers in your own header is a bad idea because it leads to circular dependencies and bloated binaries very quickly. A better approach is to use forward declaration, and include headers in your .cpp file. Recently the engine is adopting this rule a bit better, and so you typically need to add more headers to your files to access various classes.

In order to declare a UInputComponent for example with Forward Declaration, do this:

Header



// Underneath #includes
class UInputComponent;

/// MyClass code


CPP



#include "InputComponent.h"


The limitation of forward declaration is that you cannot access member variables or call functions on that class from your own header, but unless your inlining a bunch of stuff you shouldn’t need to do that anyway.

The Tutorial you are following probably uses pre-4.15 include model when everything was in monolithic headers so everything was always included anyway and you never had to worry about it.

The problem with that approach is it increases compile time drastically, so Epic moved to an include what you use (IWYU) model which speeds up compilation but requires you to include things manually now.