Forward Declaration error

Hello everyone,

I posted a question over on the UE Answer Hub regarding an error I’m getting with forward declaring UTankBarrel:

https://answers.unrealengine.com/questions/596473/class-forward-declaration-not-working-with-ufuncti.html

Basically, for some reason when I forward declare UTankBarrel and try to pass the reference into the function declaration for SetBarrel in Tank.h it gives me the following error:

Unrecognized type ‘UTankBarrel’ - type must be a UCLASS, USTRUCT or UENUM

It works fine in TankAimingComponent.h and the only difference is the UFUNCTION macro before the function. My UE4 version is 4.15.2 (a few versions up, but it shouldn’t break such basic functionality, I think)

Any help is kindly appreciated.

I managed to find the culprit:

I accidentally selected StaticMeshActor instead of Component and the resulting class ended up being ATankBarrel, not UTankBarrel so when UFUNCTION was trying to compile it couldn’t find the actual class. So UFUNCTION doesn’t work with class forward declarations unless those classes actually exist somewhere, is my conclusion.

I think forward declarations have nothing to do with the type existing in some other code as well, but I might be wrong.
The fact the forward declaration of UTankBarrel didn’t work is probably in your header file you expected a ATankBarrel for the UFUNCTION instead of the UTankBarrel.

So when the compiler sees ATankBarrel it doesn’t know what it means. That’s my guess, but I might be wrong.

EDIT: Jep, I just did a forward declaration of complete random class kwlhDFNBweskhfbshkefbshefb and then assigned this to a private variable with type kwlhDFNBweskhfbshkefbshefb. It compiled without errors.

So forward declarations only tell the compiler like, if you see this, it’s class declared as whatever type, don’t worry about it.
Of course in .cpp you need to #include, because that’s where you are going to use the implementation of it.

So the fact that UFUNCTION didn’t work, was the fact it couldn’t find any declaration of the ATankBarrel type. Even if you remove the UFUNCTION it will throw that error, I think, so it’s C++ behaviour.

Well, in another header file I did the same thing (tankAimingComponent I think it was) and it worked there and the only difference is that UFUNCTION didn’t precede the function. UFUNCTION also doesn’t like unnamed arguments in function declarations in the header file. When I deleted UFUNCTION it would compile with no problem, but I needed UFUNCTION for setting the reference and wasn’t really a workaround.

Privacy & Terms