Class forward declaration not working with UFUNCTION

Hello everyone, I was wondering if anyone could tell me why I’m getting this obscure error and how to fix it/what I’m doing wrong. So, in this code snippet:

#pragma once

#include "../Public/TankAimingComponent.h"
#include "GameFramework/Pawn.h"
#include "Tank.generated.h"

class UTankBarrel; //Forward declaration

class BATTLETANKS_API ATank : public APawn

	// Sets default values for this pawn's properties
	void AimAt(FVector);

	//Allows the blueprint to call this method
	UFUNCTION(BlueprintCallable, Category = Setup)
		void SetBarrelReference(UTankBarrel*);


	UTankAimingComponent* tankAimingComponent = NULL;

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;


	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	UPROPERTY(EditAnywhere, Category = Firing)
		float launchSpeed = 100000;


I’m making a class forward declaration and using it in a function under UFUNCTION (Line 20), which to which i am aware should work without problem. However I’m getting the following error:

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

I have the same declaration in another header file with a similar function that compiles and the only difference is it doesn’t have the UFUNCTION macro. My UE4 version is 4.15.2.

Any help is appreciated and apologies if this was asked/solved before.

I don’t really use forward declarations so forgive me if this is not the answer you are looking for. I generally declare things like this, when using game project C++ files:

     UFUNCTION(BlueprintCallable, Category = Setup)
     void SetBarrelReference(class UTankBarrel* InBarrel);

I suggest removing the class UTankBarrel declaration and change this UFUNCTION.

I tried it that way as you suggested and I’m still getting the same error, unfortunately.

Have you made a class that is UTankBarrel, which is included in the project? Or, are you just trying to declare it?

Yes, I have made a class using the Editor, however I was told this way I don’t create some problems by including header files in other header files and creating circular dependencies. As I said, in another class this works fine, it just doesn’t have UFUNCTION in front of it.

I just want to declare it so that the function in this class compiles and doesn’t need to worry about it.

Did you compile the project before trying to use the class? I know it sounds odd but I have ran into issues of trying to use a type before its been compiled and getting errors like this.

You are right that you don’t need to include the project headers in other project headers.

It’s tough to figure out why you would have this issue because as long as you have made the UTankBarrel that is a component and that the class is declared as a UCLASS( ) in the BATTLETANKS_API, it should work.

I tried building, rebuilding and cleaning the whole solution and just the project, the same error popping up each time. I shrugged it as irrelevant, but perhaps the other two errors might have to do with it or lead to a solution?

UnrealHeaderTool failed for target 'BattleTanksEditor' (platform: Win64, module info: D:\Users\<USER>\Documents\Unreal Projects\UnrealCourseSection5\BattleTanks\Intermediate\Build\Win64\BattleTanksEditor\Development\BattleTanksEditor.uhtmanifest, exit code: OtherCompilationError (5)).	BattleTanks	D:\Users\<USER>\Documents\Unreal Projects\UnrealCourseSection5\BattleTanks\Intermediate\ProjectFiles\


Error	MSB3075	The command ""C:\Program Files\Epic Games\UE_4.15\Engine\Build\BatchFiles\Build.bat" BattleTanksEditor Win64 Development "D:\Users\<USER>\Documents\Unreal Projects\UnrealCourseSection5\BattleTanks\BattleTanks.uproject" -waitmutex" exited with code 5. Please verify that you have sufficient rights to run this command.	BattleTanks	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets	41

It looks like there is more going on then the missing UCLASS, USTRUCT, UENUM error.

Generally when you get the “exited with code 5” it is a issue with a macro, or you actually don’t have permission to do that operation.

If you want, you can send me the project and I can try to look at it. You can either post it here or send me a PM on the forums.

If you do decide to send a project, you only need to include:

  1. Config
  2. Content
  3. Source
  4. .uproject

If you don’t want to send your project, just go back to where you were when you got the “exited with code 5” error and try to figure out what went wrong.

I think I have found the culprit, I selected AStaticActor instead of UStaticMeshComponent when creating UTankBarrel, will see how it goes once I fix that.

Yep, that fixed it. An incorrect class.

So, in summary you have to have a functioning class (that successfully compiles) for it to work with UFUNCTION, forward declaration by itself doesn’t work.

Thank you for your help, Kyle!

No problem, glad to help.