# Can I use a template class?

**URL:** <https://forums.unrealengine.com/t/can-i-use-a-template-class/373431>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [October 15, 2016, 9:04pm UTC](https://forums.unrealengine.com/t/can-i-use-a-template-class/373431 "2016-10-15T21:04:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon98922229](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon98922229](https://forums.unrealengine.com/u/anon98922229)\
**Post date:** [October 15, 2016, 9:04pm UTC](https://forums.unrealengine.com/t/can-i-use-a-template-class/373431/1 "2016-10-15T21:04:02Z")

</div>

Whenever I attempt to create an instance of a custom template class Unreal crashes and produces the following errors:

```
2>ActorProperties.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl MyClass<int>::~MyClass<int>(void)" (??1?$MyClass@H@@QEAA@XZ) referenced in function "int `public:__cdecl ActorPropertyStaticStore::ActorPropertyStaticStore(struct dtor$1 &&)'::`1'::dtor$1" (?dtor$1@?0???0ActorPropertyStaticStore@@QEAA@$$QEAU0@@Z@4HA)
2>ActorPropertyStaticStore.cpp.obj : error LNK2001: unresolved external symbol "public: __cdecl MyClass<int>::~MyClass<int>(void)" (??1?$MyClass@H@@QEAA@XZ)
2>Tetra_MainGameMode.cpp.obj : error LNK2001: unresolved external symbol "public: __cdecl MyClass<int>::~MyClass<int>(void)" (??1?$MyClass@H@@QEAA@XZ)
2>Tetra_Main.generated.cpp.obj : error LNK2001: unresolved external symbol "public: __cdecl MyClass<int>::~MyClass<int>(void)" (??1?$MyClass@H@@QEAA@XZ)
2>ActorPropertyStaticStore.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl MyClass<int>::MyClass<int>(void)" (??0?$MyClass@H@@QEAA@XZ) referenced in function "public:__cdecl ActorPropertyStaticStore::ActorPropertyStaticStore(void)" (??0ActorPropertyStaticStore@@QEAA@XZ)
2>\Binaries\Win64\UE4Editor-_Main.dll : fatal error LNK1120: 2 unresolved externals

#pragma once

 template<typename T>
class MY_MAIN_API MyClass
{
public:
	MyClass();
	~MyClass();
};

#include "Tetra_Main.h"
#include "MyClass.h"
template<typename T>
MyClass<T>::MyClass()
{
}
template<typename T>
MyClass<T>::~MyClass()
{
}

```

If I remove the template it works fine. Does the Unreal compiler just not support template classes?

---

<div class="post-metadata">

**Author:** ![GigasightMedia](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigasightMedia](https://forums.unrealengine.com/u/GigasightMedia)\
**Post date:** [October 16, 2016, 8:54pm UTC](https://forums.unrealengine.com/t/can-i-use-a-template-class/373431/2 "2016-10-16T20:54:50Z")

</div>

As far as I know, UObjects and AActors (and derived classes) do not support template classes. The UnrealHeaderTool attempts to resolve all symbols within the header, so you’re getting those errors.

Alternatively, you can create a UInterface and related IInterface:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

> **[Interfaces](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/GameplayArchitecture/Interfaces/)**
>
> Reference to creating and implementing interfaces.

[https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UInterface/index.html](https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UInterface/index.html)

If you don’t like that, you can create your base class with the UCLASS specifier “Abstract”, and then include a noentry or unimplemented assertion within your methods. e.g.:

```
//header

#pragma once

#include "MyAbstractClass.generated.h"

UCLASS(Abstract)
class MYGAME_API UMyAbstractClass : public UObject
{

    GENERATED_BODY;

public:

    UMyAbstractClass();

    void MyMethod();

};

//source

#include "MyAbstractClass.h"

UMyAbstractClass::UMyAbstractClass()
    :
    UObject()
{}

UMyAbstractClass::MyMethod()
{
    unimplemented();
}

```

With that structure you’ll get an error thrown if any of the methods are called without being overridden. The “Abstract” specifier prevents the engine from allowing instances of the class to be created directly, but instances of derived classes remain legal.

[https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/Specifiers/Abstract/index.html](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/Specifiers/Abstract/index.html)

> **[Asserts](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/Assertions/)**
>
> Reference to Unreal Engine 4's Assert Functionality

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Willard720](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Willard720](https://forums.unrealengine.com/u/Willard720)\
**Post date:** [January 25, 2023, 5:21pm UTC](https://forums.unrealengine.com/t/can-i-use-a-template-class/373431/3 "2023-01-25T17:21:29Z")

</div>

Forgive my ignorance, but how does interfaces or abstract classes help emulate templates? These are nothing a like.
