Trouble with Forward Declarations

In my general understanding, you add an include files you want to use stuff from another header. I am currently running into an issue of circular dependency and is using shootergame as an example. How do they get away with adding only Generated file type.

EDIT : CORRECTION, I’m looking at ShooterTypes not ShooterImpactEffect. sorry

I’m looking at ShooterTypes. How do they get away with only

#include "ShooterTypes.generated.h"

It clearly uses ShooterCharacter in line 113

TWeakObjectPtr<class AShooterCharacter> PawnInstigator;

That particular case is using a forward declaration to avoid having to include the header for that type.

This Stack Overflow answer gives a good summary of when you can and can’t use a forward declaration: c++ - When can I use a forward declaration? - Stack Overflow

I’ve read on the Forward declarations including the wiki for unreal engine.
There’s 2 problem I’m still facing:

  1. I cannot see where/ how ShooterTypes.h did the forward declaration. They didn’t seems to do any

    class AShooterCharacter ;

can you clarify how they did it?

  1. I tried doing forward declaration by just doing

    class AMyNeededclass;

on top .

However it wouldn’t let me do a

TWeakObjectPtr<class AMyNeededClass> PawnInstigator; 

Saying TWeakOBjectPtr must only be used on a UClass.

Since they demanded a UClass. I try doing

class AMyNeededClass : public ACharacter ;

this then come out with error saying "missing ‘,’ before ‘;’ "

I also tried doing

Class AMyNeededClass : public ACharacter 

I got some other error ( The class name must match the name of the class header file it is contained in (‘PCTypes’), with an appropriate prefix added (A for Actors, U for other classes) )

Right, so TWeakObjectPtr has a default constructor, and a constructor which takes a pointer. Only the latter of those performs the UObject type check, and it will have needed to see the type declaration to be able to do that (a forward declaration will not suffice).

This sounds like an issue with the instantiation of your TWeakObjectPtr rather than your declaration of it.

With regard to your question about how the the forward declaration is happening, you can do a forward declaration inline, so the following does forward declare AMyNeededClass, but only for the scope of that variable declaration (as an aside, I don’t like the inline version of forward declarations since it can cause issues with namespaces, but since UE4 doesn’t really use namespaces, it’s not so much of an issue here).

TWeakObjectPtr<class AMyNeededClass> PawnInstigator;

you are so right about this. I’ve been barking up the wrong tree all this while. It is indeed the instantiation of it.

I actually left the declaration untouched at (my version of ) ShooterTypes.h . Apparently it’s the instantiation at (my verison of ) ShooterImpactEffect that needed to add the Include.

I am currently having the same issue, I don’t follow this answer. What did you do to resolve this issue?

Add the forward declaration at the class where you want to create it. Not at the types class