While I was updating my plugin for UE5.1, I was suddenly getting linker errors pointing to my template function that had no errors before. If you are experiencing the same issue, here’s how you can fix it.
A: Move the entire template function into the .h file.
B: Explicitly instantiate the template function in the .cpp file.
// .h
template<typename ValueType>
bool GetSessionSetting(const FName SessionName, const FName Key, ValueType& OutValue);
// .cpp
template<typename ValueType>
bool UKronosOnlineSession::GetSessionSetting(const FName SessionName, const FName Key, ValueType& OutValue)
{
// Function implementation
}
// Explicit instantiation of supported types for the template above.
template KRONOS_API bool UKronosOnlineSession::GetSessionSetting(const FName SessionName, const FName Key, int32& OutValue);
template KRONOS_API bool UKronosOnlineSession::GetSessionSetting(const FName SessionName, const FName Key, float& OutValue);
template KRONOS_API bool UKronosOnlineSession::GetSessionSetting(const FName SessionName, const FName Key, FString& OutValue);
template KRONOS_API bool UKronosOnlineSession::GetSessionSetting(const FName SessionName, const FName Key, bool& OutValue);