# How to build A TMap with FString and TFunction?

**URL:** <https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 6, 2018, 8:08am UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114 "2018-11-06T08:08:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![XynanXDB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XynanXDB](https://forums.unrealengine.com/u/XynanXDB)\
**Post date:** [November 6, 2018, 8:08am UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/1 "2018-11-06T08:08:56Z")

</div>

Hi, I’m constantly having errors when I’m trying to build a TMap with FString as key and TFunction as values. Here are my codes:

**Something.h**

```
TMap<FString, TFunction<void(USomeActorComponent::*)()>> StringFunctionMap;

```

**Something.cpp**

```
USomeActorComponent::USomeActorComponent()
{
	PrimaryComponentTick.bCanEverTick = false;

	SetIsReplicated(true);
	StringFunctionMap.Add(TEXT("FunctionA"), &USomeActorComponent::BlahBlah);
}

```

**My errors**

```
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/Function.h(527) : error C2504: 'UE4Function_Private::TFunctionRefBase<TFunction<void (__cdecl USandboxComponent::* )(void)>,FuncType>': base class undefined
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: FuncType=void (__cdecl USomeActorComponent::* )(void)
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(291) : note: see reference to class template instantiation 'TFunction<void (__cdecl USomeActorComponent::* )(void)>' being compiled
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(304) : note: see reference to class template instantiation 'TCallTraitsBase<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(357) : note: see reference to class template instantiation 'TCallTraits<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(368) : note: see reference to class template instantiation 'TTypeTraitsBase<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USandboxComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(114) : note: see reference to class template instantiation 'TTypeTraits<ValueType>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: ValueType=TFunction<void (__cdecl USandboxComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(752) : note: see reference to class template instantiation 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>,
CompilerResultsLog: SetAllocator=FDefaultSetAllocator,
CompilerResultsLog: KeyFuncs=TDefaultMapHashableKeyFuncs<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,false>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(859) : note: see reference to class template instantiation 'TSortableMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>,
CompilerResultsLog: SetAllocator=FDefaultSetAllocator,
CompilerResultsLog: KeyFuncs=TDefaultMapHashableKeyFuncs<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,false>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\SVN\Source\ProjectCombine\Public\Components\USomeActorComponent.h(37) : note: see reference to class template instantiation 'TMap<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<KeyType,ValueType,false>>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/Function.h(527) : error C2504: 'UE4Function_Private::TFunctionRefBase<TFunction<void (__cdecl USomeActorComponent::* )(void)>,FuncType>': base class undefined
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: FuncType=void (__cdecl USomeActorComponent::* )(void)
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(291) : note: see reference to class template instantiation 'TFunction<void (__cdecl USomeActorComponent::* )(void)>' being compiled
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(304) : note: see reference to class template instantiation 'TCallTraitsBase<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(357) : note: see reference to class template instantiation 'TCallTraits<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Templates/UnrealTypeTraits.h(368) : note: see reference to class template instantiation 'TTypeTraitsBase<T>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: T=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(114) : note: see reference to class template instantiation 'TTypeTraits<ValueType>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(752) : note: see reference to class template instantiation 'TMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>,
CompilerResultsLog: SetAllocator=FDefaultSetAllocator,
CompilerResultsLog: KeyFuncs=TDefaultMapHashableKeyFuncs<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,false>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\Epic Games\Engine\Source\Runtime\Core\Public\Containers/Map.h(859) : note: see reference to class template instantiation 'TSortableMapBase<KeyType,ValueType,SetAllocator,KeyFuncs>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>,
CompilerResultsLog: SetAllocator=FDefaultSetAllocator,
CompilerResultsLog: KeyFuncs=TDefaultMapHashableKeyFuncs<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,false>
CompilerResultsLog: ]
CompilerResultsLog: Error: D:\SVN\Source\ProjectCombine\Public/Components/USomeActorComponent.h(37) : note: see reference to class template instantiation 'TMap<FString,TFunction<void (__cdecl USomeActorComponent::* )(void)>,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<KeyType,ValueType,false>>' being compiled
CompilerResultsLog: with
CompilerResultsLog: [
CompilerResultsLog: KeyType=FString,
CompilerResultsLog: ValueType=TFunction<void (__cdecl USomeActorComponent::* )(void)>
CompilerResultsLog: ]
CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: D:\SVN\Binaries\Win64\UE4Editor-ProjectCombine-3410.dll
CompilerResultsLog: Total build time: 32.55 seconds (Local executor: 0.00 seconds)

```

Any help will be appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![mcleary](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mcleary/32/924204_2.png) [@mcleary](https://forums.unrealengine.com/u/mcleary)\
**Post date:** [November 6, 2018, 11:54am UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/2 "2018-11-06T11:54:49Z")

</div>

Are you defining the Map as a member of your `USomeActorComponent` class?

---

<div class="post-metadata">

**Author:** ![XynanXDB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XynanXDB](https://forums.unrealengine.com/u/XynanXDB)\
**Post date:** [November 6, 2018, 12:09pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/3 "2018-11-06T12:09:55Z")

</div>

Yes I am, I wanted to create a map to that take a string as a key to call function because my colleague say if I use switch (which I’m using now) the function become very big. I’m thinking if I can do like  
(void\*()map[“key”])()  
it would be nice.

---

<div class="post-metadata">

**Author:** ![mcleary](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mcleary/32/924204_2.png) [@mcleary](https://forums.unrealengine.com/u/mcleary)\
**Post date:** [November 6, 2018, 12:20pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/4 "2018-11-06T12:20:37Z")

</div>

I cannot say right now what is wrong with the code, but my guess is that the compiler does not have the definition for the function type you want to store or `TFunction` might not be compatible with containers, but I am not sure, I have to dig a little bit more.

Be aware however that defining a `TFunction` requires the exact function signature, so all functions stored will have to match the signature. You can not have a method that takes and int for example in your code.

My best advice right now is for you to take a look into delegates: [Delegates | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Delegates)

“Delegates allow you to call member functions on C++ objects in a generic, yet type-safe way”.  
Be also aware that any technique like this is probably slower than calling the functions in a switch as you said.

---

<div class="post-metadata">

**Author:** ![XynanXDB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XynanXDB](https://forums.unrealengine.com/u/XynanXDB)\
**Post date:** [November 6, 2018, 12:36pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/5 "2018-11-06T12:36:42Z")

</div>

All the function I’m storing are virtual void() so, it should have any issue? Or does virtual keyword matters?

---

<div class="post-metadata">

**Author:** ![mcleary](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mcleary/32/924204_2.png) [@mcleary](https://forums.unrealengine.com/u/mcleary)\
**Post date:** [November 6, 2018, 3:04pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/6 "2018-11-06T15:04:27Z")

</div>

This is only me trying to get a full picture of what you are trying to do. Are you going to override these functions in child classes?

---

<div class="post-metadata">

**Author:** ![XynanXDB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XynanXDB](https://forums.unrealengine.com/u/XynanXDB)\
**Post date:** [November 6, 2018, 3:09pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/7 "2018-11-06T15:09:00Z")

</div>

The virtual void function have their own respective body in the cpp file, I’m making them as virtual void in case I have to override them in the future.

---

<div class="post-metadata">

**Author:** ![mcleary](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mcleary/32/924204_2.png) [@mcleary](https://forums.unrealengine.com/u/mcleary)\
**Post date:** [November 6, 2018, 9:03pm UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/8 "2018-11-06T21:03:37Z")

</div>

So, I was experimenting a little bit with this idea and I came up with a solution that does what you expect.

When defining your map, do it like this:

```
TMap<FString, TFunction<void(USomeActorComponent*)>> StringFunctionMap;

```

In this way you are storing a pointer to a function whose first argument is a pointer to `USomeActorComponent`. You just need to remember that a member function is just a function where the first parameter is the `this` you can use inside the function.

You can create the map in the same way:

```
StringFunctionMap.Add(TEXT("FunctionA"), &USomeActorComponent::BlahBlah);

```

The only difference will be when you call this function:

```
StringFunctionMap["FunctionA"](this);

```

You just need to remember to pass the `this`.

---

<div class="post-metadata">

**Author:** ![XynanXDB](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@XynanXDB](https://forums.unrealengine.com/u/XynanXDB)\
**Post date:** [November 7, 2018, 3:31am UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/9 "2018-11-07T03:31:27Z")

</div>

Thank you very much man, you’re are a life saver. The TFunction made me dizzy haha.

---

<div class="post-metadata">

**Author:** ![mcleary](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mcleary/32/924204_2.png) [@mcleary](https://forums.unrealengine.com/u/mcleary)\
**Post date:** [November 7, 2018, 9:50am UTC](https://forums.unrealengine.com/t/how-to-build-a-tmap-with-fstring-and-tfunction/437114/10 "2018-11-07T09:50:06Z")

</div>

Be aware that I have not tested this for an overridden function.
