Why Forward Declarations vs. #include?

What’s the advantage of using forward class declarations over using #include “TankBarrel.h”…

It seems I must be missing something, because while

UTankBarrel *Barrel;

works, in the CPP file,

 Barrel->Elevate(0.0f);

returns the error message:

Pointer to incomplete class not allowed.

If I remove the forward declaration and simply use

 #include "TankBarrel.h"

then it compiles perfectly.

1 Like

Because the header file doesn’t need to know anything beyond the fact that UTankBarrel is a class, while the cpp file needs to know the members of the class since you are using one if not more of its members (UTankBarrel::Elevate)

The reason for using them is because it’s extra work for the preprocessor if you don’t. Say you include TankBarrel.h in the header instead of a forward declaration. This means that any file that wants to include Tank.h also has to include TankBarrel.h even it doesn’t need it.

The solution to your problem is to include it in the cpp file and forward declare in the header

1 Like

I missed seeing that declaration in the .cpp file, somehow. Thanks. I’ll
adjust the code when I get home and test it for myself.
That does make sense, and quite literally ANYTHING we can do to speed up
compiling and Intellisense is something I should get in the practice of
doing.

Ok, got it working correctly, but… something I’m noticing…
every time I set up a forward reference, Intellisense BREAKS… it can’t make heads or tails out of the main class declaration, and I have to pretty much restart Visual Studio to make it work again. Anybody have any ideas why that’s happening? (VS 2017)
The program will still compile just fine, but Intellisense won’t work properly.

Still wondering about the Intellisense breaking when using a /class UForwardDeclaration…

The main object class gets squiggly, and an overmouse gives you:

 This declaration has no storage class or type specifier

The solution appears to be to rescan solution, which basically means run down to Starbucks and get a Venti Unicorn Frappuccino while you wait…
The project will still compile, and you can still edit code, just no intellisense for help.

Where are you getting those errors?

In Intellisense, any time I type a forward declare.
The UCLASS, and then pretty much every non keyword after that gets the error squiggle, once the Intellisense catches up to the fact you typed something. Then when you mouseover the UCLASS, or look on the Error pane, that’s the error message.
It compiles fine, but even then, Intellisense still shows the error. So far, the only fix I’ve found is to Rescan Solution.

Can you show a screenshot of that? Doesn’t sound right

Hey Brian,

Are you using VS2017?
I’m having the same issue with VS2017, compiles fine, but error messages all over in VS as soon as I added the forward class declaration.

I read somewhere the error list in visual studio should be disabled, because it gives false positives with UE4.

@DanM Basically, the only thing that changed about this is the class AProjectile; Annoyingly, now even a ReScan Solution won’t bring Intellisense back to this file.

@Kevin_DB Yes, VS2017.

Ok, I’ve observed this enough to at least keep it from taking me all day to get back to a working state…
Type your class declaration.
click build within Visual Studio BEFORE Intellisense catches up.
It should compile, but then Intellisense will still show the error…
Rescan Solution
Then start adding the rest of the code and referencing the new class.
(Visual Studio 17)

my solution for intellisense for VS 2017 is to close VS 2017, hit compile within Unreal Editor, then File->Open Visual Studio within the editor and intellisense works again. This seems to be an issue with VS not being able to keep track of everything. The paid plugin Visual Assist by Whole Tomato Software is the industry accepted solution to this issue. The problem is due to the complexity of the Unreal Engine itself. If you plan on making money from Unreal in the future, this extension is a must! If you are just learning for a hobby, the closing VS, compile within the editor, then open VS from within the editor is my best suggestion.

I fixed the majority of my intellisense issues by deleting all of my vs and project files that can be regenerated by the UProject file. This seemed to fix almost all of my issues, there is still some red lines under one or two of my GetWorld()'s but that is it. The code has always compiled in UE and build in VS.

Privacy & Terms