Why do I get this error saying I am not initializing a const?

The error I get is : error C2789: ‘ACameraDirector::SmoothBlendTime’: an object of const-qualified type must be initialized

I get it for both const variables.

This is part of the header file. I have two const float variables declared in my class.

SmoothBlendTime and TimeBetweenCameraChanges;

#pragma once

#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"

UCLASS()
class CAMS_API ACameraDirector : public AActor  
{
    GENERATED_BODY()
    private:
	    APlayerController* OurPlayerController;
	    const float SmoothBlendTime;
	    const float TimeBetweenCameraChanges; 
    public:
	    // Sets default values for this actor's properties
    	ACameraDirector();

        ... (more methods/attributes)

	
};

This is my default constructor definition in the cpp file

ACameraDirector::ACameraDirector() : SmoothBlendTime(0.75f), TimeBetweenCameraChanges(2.0f)
{
	PrimaryActorTick.bCanEverTick = true;
}

Why do I get this error? I found out other ways to initialize the variables so I’m not looking for any solution for my problem. What I’m searching is why this cannot be done this way (If i recall correctly this is a standard way to initialize const variables in c++, but I am new to unreal anyway).

You need to specify a value for const member variable, It is actually not very common (afaik) to use const member variable in c++, you can either use a #define preprocessor, a static const member variable, or a const variable in a namespace :

In the game, however, I usually put these ‘const’ config values that are used by many object instances, in the Game Singleton. Or, if I want the user to change it via ini file (quite common in mod-able games) then I will put them in a special config class :

I have specified a value for the const member variable, check the constructor initialization list.

I have always initialized const variables this way and it worked. What I am asking is not some way to circumvent the problem but why it doesn’t work here. Is it a matter of C++11 or some kind of unreal issue?

Two sources that show how to initialize const variables in c++ the way I did it:

It’s possible that you’re getting the error as a result of the following generated constructor:

 ACameraDirector(const FObjectInitializer& ObjInitializer);

This is defined by UHT (Unreal Header Tool) to correctly initialize UObjects (and by extension, AActors). If you change your default constructor to this signature, UHT will stop generating it for you, and your error should go away.

ACameraDirector::ACameraDirector(const FObjectInitializer& ObjectInitializer)
  : Super(ObjectInitializer)
  , SmoothBlendTime(0.75f)
  , TimeBetweenCameraChanges(2.0f)
 {
     PrimaryActorTick.bCanEverTick = true;
 }

I did what you suggested yet i still get the same error about not initializing them.

hmm - that’s puzzling :confused:

Might you be able to post a code snippet of the offending code?

Well if you want more parts of my code( even though i think it would be useless) or the exact error i get let me know. Here is my whole header file and the implementation of the constructor in the cpp file. The const variables were renamed to x and y.

Header file

#pragma once

 #include "GameFramework/Actor.h"
 #include "CameraDirector.generated.h"

UCLASS()
class CAMS_API ACameraDirector : public AActor  
{
    GENERATED_BODY()
    private:
	struct Camera
	{
		AActor *CameraPtr;
		float SmoothBlendTime;
		float TimeBetweenCameraChanges;
		Camera(AActor *ptr, float Sbt, float Tbcc)
		{
			CameraPtr = ptr;
			SmoothBlendTime = Sbt;
			TimeBetweenCameraChanges = Tbcc;
		}
	};

	APlayerController* OurPlayerController;
	TArray<Camera> Cams;
	int camIndex;
	int CamNum;

	const float x;
	const float y;

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

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

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

	float TimeToNextCameraChange;

	
};

CPP file with the constructor only

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

 #include "Cams.h"
 #include "CameraDirector.h"
 #include "Kismet/GameplayStatics.h"



// Sets default values
ACameraDirector::ACameraDirector(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
    , x(0.75f)
    , y(2.0f)
{
	PrimaryActorTick.bCanEverTick = true;
}

Ah, it’s possible (for serialization reasons) you need to define both the ACameraDirector(const FObjectInitializer&) and ACameraDirector() constructors. Normally this issue wouldn’t show up with most classes since all types are generally default-constructable.