Trying to use a template function e.g. FindAndAssign<T>(AActor* Owner, T*& ToProp)

This isn’t exactly following the video, but I wanted to try to use a template function to do the “find and assign” operations which are already repetitive with 2 components.

Following the example here:

https://wiki.unrealengine.com/Templates_in_C%2B%2B

…I defined the function inline in my .h file. But I haven’t been able to get any variation of it to compile.

Anyone who understands template functions in C++/Unreal can take a look and let me know how to fix?

OS: OSX 10.13.3
XCode: 9.3
Unreal: 4.19.1

This is my Grabber.h file that contains the template function:

This is the log of errors I see:

EXPECTATION

To compile this function and then be able to call it from Grabber.cpp to FindAndAssign PhysicsHandle and Input components

OBSERVED

Template function fails to compile with errors in gist above

WHAT I’VE TRIED

  • put the template function in .h or .cpp files
  • completely stub out the template function, leaving just the signature w empty body
  • add/remove ‘static’ and ‘FORCEINLINE’ in all combinations
  • compile a minimal template function, e.g. template FORCEINLINE void Foo(T p) {}

OK. The problem was my bad syntax. In case anyone’s interested, this is the correct syntax

template <typename T>
FORCEINLINE bool FindAndAssign(T*& AssignToProperty, FString CompName)
{
    T* Comp = GetOwner()->FindComponentByClass<T>();

    if(Comp) {
        UE_LOG(LogTemp, Warning, TEXT("Found and assigned required component %s"), *CompName);
        AssignToProperty = Comp;
        return true;
    }
    
    UE_LOG(LogTemp, Error, TEXT("Missing required component %s"), *CompName);
    return false;
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms