Syntax error: missing ';' before '*' (First Person Shooter C++ Tutorial)

Hi,

I’m following the official tutorial for making an FPS in C++, and am currently on the ‘Changing the Camera View’ step. After entering the header code for the character controller as the tutorial instructs:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"

UCLASS()
class FPSPROJECT_API AFPSCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	AFPSCharacter();
	// Sets default values for this character's properties
	AFPSCharacter(const FObjectInitializer& ObjectInitializer);

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

	// Called when the player takes posession of a pawn.
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	// Handles moving backward and forward.
	UFUNCTION()
		void MoveForward(float Val);

	// Handles moving left and right.
	UFUNCTION()
		void MoveRight(float Val);

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

	// Sets jump flag when key is pressed.
	UFUNCTION()
	void OnStartJump();

	// CLears jump flag when key is released.
	UFUNCTION()
	void OnStopJump();

	// First person camera property.
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	UCameraComponent* FirstPersonCameraComponent;	// THE PROBLEM
	
};

… I’m getting this error:

FPSCharacter.h(48): error C2143: syntax error: missing ‘;’ before ‘*’

On some of the C++ questions online relating to this error, I’ve seen fixes regarding the #includes. My header file is just including the typical auto-generated stuff:

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"

… while my cpp file also has the Engine include:

#include "FPSCharacter.h"
#include "Engine.h"

But I’m not really sure if that’s the problem here. Can anyone help?

You need to Forward Declare your UCameraComponent so the header knows that you specify this class in your .cpp. You either write

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
class UCameraComponent* FirstPersonCameraComponent;

or even better declare on top, so if you have multiple components of the same type, you don’t have to write it twice. Also, it looks better when you forward declare on top like so:

#pragma once
 
 #include "CoreMinimal.h"
 #include "GameFramework/Character.h"
 #include "FPSCharacter.generated.h"
 
class UCameraComponent;

 UCLASS()

...

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
UCameraComponent* FirstPersonCameraComponent;

Perfect! That did the trick.

I’ve done some googling on forward declarations, and honestly I can’t figure out why I need one here in particular. Isn’t UCameraComponent just a standard UE4 class, and therefore I’m not defining it at all? Or do I need to give my header the forward declaration because the #include which contains UCameraComponent is in the cpp.? In this case, can I ask why I can’t just put the #include for Engine.g in the header? And why didn’t the tutorial mention this?

You didn’t mark which engine version you are using, so I assume 4.16+. The engine changed to a Include What You Use model (IWYU), more information here. That means most tutorials need to be updated. The above link also explains why you don’t want to include most standard stuff into engine.h. If you read the link and got more questions, just report back. <3

Great. I’m giving this a try to see if I can achieve that “ridiculously short compile time”. However, I’ve found that one variable I’m using (GEngine) is only available from Engine.h, one of the monolithic includes Unreal warns against. Assuming I can’t get it from elsewhere, does having just one Engine/h include slow my compile time as much as 5 of them?

Thanks again for your help.

Didn’t test it, but never include Engine.h. If you need GEngine for Debug messages for example, use Engine/Engine.h, that’s a big difference.