Error when using a struct as the values in a TMap.

Hi!

I’m using Unreal Engine 5.2 with C++ and Visual Studio Enterprise 2022.

I have declared this TMap:

private:
	UPROPERTY()
	TMap<int32, FBall> BallMap;

The struct is:

USTRUCT(BlueprintType)
struct FBall
{
	GENERATED_BODY()

	int32 Hip;
	bool ABoolean;
	double DoubleValue1;
	double DoubleValue2;
	double Mag;
	FString StringValue1;
	FString StringValue2;
	FString StringValue3;
	double DoubleValue3;

	UPROPERTY()
	class ABallActor* BallActor = nullptr;

	FStruct1 StructureValue1;
	FVector ALocation;

	FStruct2 StructureValue2;
	double DoubleValue4;
};

When I add a new value to the TMap:

while (PreparedStatement->Step() == ESQLitePreparedStatementStepResult::Row)
{
	Fball ball;

	PreparedStatement->GetColumnValueByName(TEXT("HIP"), ball.Hip);
	[...]
	
	BallsMap.Add(ball.Hip, ball);
}

I get this error:

G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): error C2663: 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add': no overloaded function has a valid cast for 'this' pointer
         with
         [
             KeyType=int32,
             ValueType=FBall,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Games\Epic Games\UE_5.2\Engine\Source\Runtime\Core\Public\Containers\Map.h(365): note: can be 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add (KeyType &&,ValueType &&)'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add(KeyType &&,ValueT ype &&) ': cannot convert pointer 'this' from 'const TMap<int32,FBall,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>' to 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs> &'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
         and
         [
             InKeyType=int32,
             InValueType=FBall
         ]
         and
         [
             KeyType=int32,
             ValueType=FBall,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: Lost qualifiers on conversion
G:\Games\Epic Games\UE_5.2\Engine\Source\Runtime\Core\Public\Containers\Map.h(364): note: o 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add( KeyType &&,const ValueType &)'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add(KeyType &&,const Value Type & )': Cannot convert pointer 'this' from 'const TMap<int32,FBall,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>' to 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs> &'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
         and
         [
             InKeyType=int32,
             InValueType=FBall
         ]
         and
         [
             KeyType=int32,
             ValueType=FBall,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: Lost qualifiers on conversion
G:\Games\Epic Games\UE_5.2\Engine\Source\Runtime\Core\Public\Containers\Map.h(363): note: o 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add( const KeyType &,ValueType &&)'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add(const KeyType &,ValueT ype && )': Cannot convert pointer 'this' from 'const TMap<int32,FBall,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>' to 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs> &'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
         and
         [
             InKeyType=int32,
             InValueType=FBall
         ]
         and
         [
             KeyType=int32,
             ValueType=FBall,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: Lost qualifiers on conversion
G:\Games\Epic Games\UE_5.2\Engine\Source\Runtime\Core\Public\Containers\Map.h(362): note:or 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add(const KeyType &,const ValueType &)'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: 'ValueType &TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::Add(const KeyType &,const Value type &)': Cannot convert pointer 'this' from 'const TMap<int32,FBall,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>' to 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs> &'
         with
         [
             ValueType=FBall,
             KeyType=int32,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
         and
         [
             InKeyType=int32,
             InValueType=FBall
         ]
         and
         [
             KeyType=int32,
             ValueType=FBall,
             SetAllocator=FDefaultSetAllocator,
             KeyFuncs=TDefaultMapHashableKeyFuncs<int32,FBall,false>
         ]
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: Lost qualifiers on conversion
G:\Sources\Repos\MyProject\Unreal\MyProjectTest\Source\MyProject\Private\CatalogueGameInstanceSubsystem.cpp(353): note: when matching argument list '(int32, FBall)'

I don’t know how to solve it.

How can I fix this problem?

Thanks!

I don’t see Hip as a member of FBall.

Are you executing this code in a const member function by any chance ?

I’ve found the solution: the method is const and I’m modifying a member of the class.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.