How can a forward declaration cause "undefined"

How is this error even possible? The whole point of “class” in front of the type is so that I do not have to include it in the header (to prevent circular references and such). However VS2017 throws an undefined-type-error? I don’t understand.

1 Like

TryRemoving the " =nullptr" Also, check if your class really exist somewhere ( name typo )

Removing the nullptr did not fix it. Also it is spelled correctly.

This code worked fine until I added another property to it:

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "BaseStructure|Components")
UBoxComponent* RootCollisionComp;

And ever since I have this problem. Outcommenting the property also does not fix it and even when I ctrl+z it back to the original state the c++ used to be last time it compiled it still throws this error. So weird.

So I added it to the header file but kept the “class” keyword and now it compiles:

#include "Structures/Ghosts/BasePlacementGhost.h"

I’m pretty sure now that this is a bug but reporting such a hard to reproduce bug probably won’t do much. It very much looks like the forward declaration being bugged. But it is reproducible 100% of the time in my project it seems :confused:

I also noticed the compiler having some problems with it. Compiling the class that inherits this class t akes about 30 seconds for ONE tiny class. Really odd.

Another example: A moment ago all I did was delete some unused starter content and suddenly a completely unrelated c++ filed started complaining about a missing include.

Now he says that he got these issues as well after migrating to 4.18. What he said:

he adds the newly edited file to the
adaptive build

my guess is that the compiler of 4.18
is more strict in terms of includes,
so when you edit a file he’s forced to
recompile that and apply the new
rules, while the others were still
built with 4.17

the way i got to fix all that problems
was to delete all of the intermediate
folders and recompile the whole thing
from scratch and i got all of the
problems in a row

I can confirm that cleaning up the project and regenerating the VS files often fixes the problem (but for me sometimes only temporary). I still believe that this is a bug from UE4. A small one yes but an annoying one at times. The best fix (for me) seems to be to just add any include it wants, even if it means including something twice. At least UE4 then let’s me compile it and the issue does not return. Note that my project was not migrated but also has the same issues.

However this should be reported as a bug but I cannot reproduce it without sending my entire project over to Epic I fear and thus reporting it is pointless… My project does however reproduce this 100% of the time.

At least I’m not the only one with these problems.

Migrating a C++ project from 4.15 to 4.18 also created a lot of problems with forward declarations as well as (very strange) the chain of inclusion of header files. I am not quite sure why but somehow I feel like the recent changes with the generated VS files may be responsible…

However, do consult / refresh your memory on forward declaration with this article, in particular:

It is very important to note that you
can add the required #include’s in any
.cpp file, it is only the .h files
that have to compile without circular

Just to let you know that after reverting my project from 4.18 to 4.15 (with subversion), and doing it again the exact same way… It actually worked this time and no problem with the inclusion of header files, whereas the first time, I had to manually change several header inclusions…

Conclusion: always use a versioning system with UE4

1 Like