Const int32* to int32 conversion?

Hello,

I am having a problem in this piece of code:

RemoveItemByOrdinality(*InventoryMap.FindKey(Slot));

In which “InventoryMap” is defined as:

TMap<int32, struct FInventorySlot> InventoryMap;

and the argument “RemoveItemByOrdinality” function takes is int32 like follows:

virtual void RemoveItemByOrdinality(int32 Ordinality);

Since the FindKey function in map returns a constant int32 pointer, it should be convertable to an int32 using a * in the front

*InventoryMap.FindKey(Slot)

however this causes errors in the compiling process that look like this:

1>  C:\Users\Onat\Documents\Unreal Projects\Game\Source\Game\Inventory\Inventory.cpp(160): note: see reference to function template instantiation 'const int32 *TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>::FindKey(const FInventorySlot &) const' being compiled
1>          with
1>          [
1>              KeyType=int32,
1>              ValueType=FInventorySlot,
1>              SetAllocator=FDefaultSetAllocator,
1>              KeyFuncs=TDefaultMapKeyFuncs<int32,FInventorySlot,false>
1>          ]
1>  C:\UE4\Unreal Engine\4.12\Engine\Source\Runtime\Core\Public\Containers\Map.h(810): note: see reference to class template instantiation 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
1>          with
1>          [
1>              KeyType=int32,
1>              ValueType=FInventorySlot,
1>              SetAllocator=FDefaultSetAllocator,
1>              KeyFuncs=TDefaultMapKeyFuncs<int32,FInventorySlot,false>
1>          ]
1>  C:\UE4\Unreal Engine\4.12\Engine\Source\Runtime\Core\Public\Containers\Map.h(929): note: see reference to class template instantiation 'TSortableMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
1>          with
1>          [
1>              KeyType=int32,
1>              ValueType=FInventorySlot,
1>              SetAllocator=FDefaultSetAllocator,
1>              KeyFuncs=TDefaultMapKeyFuncs<int32,FInventorySlot,false>
1>          ]
1>  c:\users\onat\documents\unreal projects\game\source\game\character\../Inventory/Inventory.h(33): note: see reference to class template instantiation 'TMap<int32,FInventorySlot,FDefaultSetAllocator,TDefaultMapKeyFuncs<KeyType,ValueType,false>>' being compiled
1>          with
1>          [
1>              KeyType=int32,
1>              ValueType=FInventorySlot
1>          ]

Could anyone tell me what I’m doing wrong?

Any help will be appreciated,

Thanks!

You were half way right with *InventoryMap.FindKey(Slot) and saying a const int32* should be convertable with a *

But it’s like this: InventoryMap.FindKey(*Slot)

The problem doesn’t appear to be the int32, but rather the struct you’re trying to use with FindKey. I assume you’re not overloading the == operator. Here’s a quick example doing so…

USTRUCT()
struct FTestStruct
{
    GENERATED_USTRUCT_BODY();

    int32 ID;
    int32 TestValue;

    FORCEINLINE bool operator==(const FTestStruct& Other) const
    {
        return ID == Other.ID;
    }
};

That doesn’t really work out since FindKey doesn’t take pointers as arguments.

Overloading the operator fixed it, thanks! Didn’t know I was supposed to do that.

Oops, I misread it as ‘takes a …’ rather than return. :slight_smile: