Can I use a template class?

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?

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:

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

Hope that helps.

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

2 Likes