-1

Hello I am trying to create a static method for checking null pointers in Unreal Engine.

.h file

template<typename T>
    static bool checkForNull(T pointer);

.cpp file

bool MyClass::checkForNull(T pointer)
{
    {
        if (!pointer) {
            printlog(FString("Your pointer is null")); //Another static function for printing
            return false;
        }
        else {
            return true;
        }
    }
}

But I am also new to C++ and I think there is a syntax error in my code? How can I create a static generic method?

user3160302
  • 227
  • 1
  • 4
  • 11
  • 1
    If there's a syntax error, there's an error message, which we would be happy to read. – cadaniluk Jan 30 '17 at 10:47
  • Questions seeking debugging help (‘**why isn't this code working?**’) must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Biffen Jan 30 '17 at 10:47
  • 2
    Use a `T*` parameter instead to enforce pointers to be passed, not arbitrary objects. – cadaniluk Jan 30 '17 at 10:51
  • 2
    "Your pointer is null" is not a very usefullog message. Which pointer is null? – n. m. could be an AI Jan 30 '17 at 11:02
  • 1
    Possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Mike Kinghan Jan 30 '17 at 11:36

1 Answers1

1

The problem is that you must put the code for your template method in the header, so that when including the header to instanciate the template, compiler would be able to build the appropriate code from the template. You must also repeat template <class T> before the definition.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69