Error when adding item to a TMap: "User-defined literal operator not found"

Hi,

new to programming with C++ and struggling to create a TMap with Fstring as Key and AnimMontage as value.

I’m trying to add the AnimMontage 104_montage and the error “User-defined literal operator not found” appears.

I’ve attached my cpp and .h files below.

Any help would be apprecited.
Thanks,
Mark

AnimTMap.H

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AnimTMap.generated.h"

UCLASS()
class MYPROJECT_API AAnimTMap : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AAnimTMap();

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TMap<FString, UAnimMontage*> AnimationMap;

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

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

};

AnimTMap.cpp

#include "AnimTMap.h"

// Sets default values
AAnimTMap::AAnimTMap()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AAnimTMap::BeginPlay()
{
	Super::BeginPlay(); 
	AnimationMap.Add("hello",104_montage);
}

// Called every frame
void AAnimTMap::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

You need to set your referance properly to 104_montage c++ wouldnt know if you are referencing an asset from the editor. For that purpose you need to use ConstructorHelpers::FObjectFinder

First go and copy referance from editor (since I dont have it I will go over some example.)

This will give you as copy something like below :

AnimMontage'/Game/Characters/XYZ_AnimMontage.XYZ_AnimMontage'

with that copy you will do as follows

include "UObject/ConstructorHelpers.h"
...
...
...
void AAnimTMap::BeginPlay()
 {
     Super::BeginPlay();
     static ConstructorHelpers::FObjectFinder<UAnimMontage> AnimMontageRef(TEXT("AnimMontage'/Game/Characters/XYZ_AnimMontage.XYZ_AnimMontage'"));
	if (AnimMontageRef.Succeeded())
		AnimationMap.Add("hello",AnimMontageRef.object);     
 }

If you have any concerns you can look up samples of ConstructorHelpers::FObjectFinder

Thanks for the response.

So when I try to access the TMap AnimationMap in a blueprint the error “FObjectFinders can’t be used outside of constructors” appears.

Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/CoreUObject/Private/UObject/UObjectGlobals.cpp] [Line: 3287] FObjectFinders can’t be used outside of constructors to find /Game/Animations/Animations/104_Montage.104_Montage

UE4Editor_CoreUObject
UE4Editor_MyProject_7751!AAnimTMap::BeginPlay() [C:\Users\Administrator\Documents\Unreal Projects\MyProject\Source\MyProject\Private\AnimTMap.cpp:18]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

So after a bit of googling I came up with the following solution to use LoadObject instead.

UAnimMontage* MontageHello = LoadObject<UAnimMontage>(NULL, TEXT("AnimMontage'/Game/Animations/Animations/104_Montage.104_Montage'"));

AnimationMap.Add("hello", MontageHello);

Thanks for leading me down the right path and if there is a better solution please let me know.