How to correctly handle includes?

I’ve been following the C++ FPS tutorial and I want to add a USphereComponent to an actor called Projectile.
However, when add it to the header file, it tells me it doesn’t know the USphereComponent.
The tutorial does not indicate that anything should be included. Is that because of the tutorial being slightly outdated?

Also, if I want to include any class, do you simply look up the C++ API and copy the given header file into your includes? In this case, #include “Runtime/Engine/Classes/Components/SphereComponent.h”?

I mean: is it normal to not have any idea what to include without the documentation? Is there no way to find out in Visual Studio?
When I use the auto suggestion feature and type “Run” it will not even give me a suggestion to “Runtime”, even though it does work. The already included header “CoreMinimal.h” is also not given per auto suggestions. Why is that? How would I know I even COULD include CoreMinimal.h if auto suggestion does not work?

Most of the time, you just include the exact header with the declaration of the required class.

Also, i can recommend “Visual assist” to improve overall programming experience, speed, memory and CPU requirements of visual studio. While it has trial for a month, it is really hard to go back from it when the trial ends, so you kinda have to be ready to spend 100$ for a license.

I see. So is it normal to actually look up the docs or is there actually a way to, let’s say, write down “USphereComponent* collisionComp;” in a header file, then automatically include the necessary header?

Because it’s pretty annoying having to look up all the headers all the time. I will look into Visual Assist though!

I have installed Visual Assist and it seems quite nice. The auto include function does not seem to work reliably though?
When I hover “USphereComponent” (which is not included yet) I can include “SphereComponent.h”, which is actually the correct header. When I compile, however, it gives me errors. SphereComponent.h: No such file or directory. Is there a way to fix this?

No auto include in c++ (maybe there are some plugins but don’t bother really)

#include "Components/SphereComponent.h"

try to use a part of the path as well.

Because it’s pretty annoying having to look up all the headers all the time. I will look into Visual Assist though!

They are not that annoying, generic stuff is already there, it’s like up to 5 files to include once, also not all of them have to be included in a header.

I guess I will just have to live with it. :slight_smile:

It makes me a little sad because Visual Assist does offer auto include and it DOES include the correct file; just not the correct path… which seems like a minor issue which would speed up the minor annoyances quite a bit.

I included that, and it underlined the “GENERATED_BODY()” line, saying “this declaration has no storage class or type specifier”.

Visual Assist is recommended from most people but I program perfectly fine without it. I barely get false intellisense errors once you follow a few general rules.

  1. Always forward declare every header for classes used as member variables (in header).
  2. If false errors persist, generate vs project files.
  3. The auto-complete won’t work if the header for that specific class is not included so include that header in .cpp

I included the headers I needed in the cpp and that cleared up all the intellisense errors, but now the project won’t build and there are all kinds of weird errors. “syntax error: missing ‘;’ before ‘*’”, “unexpected token(s) preceding ‘;’”, etc. The line is

UPROPERTY(VisibleDefaultsOnly, Category = Projectile) USphereComponent* CollisionComponent;

It would help if you could provide some more code, as the error could be in close proximity to this line. Also the precise error message would be helpful.

Sure, here’s a Pastebin with the full code: FPSProjectile.h - Pastebin.com

I included Components/SphereComponent.h and GameFramework/ProjectileMovementComponent.h in FPSProjectile.cpp

Here are a couple of the errors from line 27:

C2143	syntax error: missing ';' before '*'
C4430	missing type specifier - int assumed. Note: C++ does not support default-int
C2238	unexpected token(s) preceding ';'

The full list of 86 errors is here: FPSProjectile errors - Pastebin.com

The Component Types are unknown to the compiler. Try to forward-declarate them.
Therefore add the following above your class:

class USphereComponent;
class UProjectileMovementComponent;

Show the whole file.

That solved it, thanks!