views:

60

answers:

2

How can I use a static function pointer member in my class template?

I'm working with C++ in Visual Studio, and my code looks similar to the following:

template<typename T>
class ClassTemplate
{
public:
    static T* CallIt() { return ClassTemplate<T>::mFunctionPointer(); }

private:
    static T* (*mFunctionPointer)();
};

When I compile I get an "unresolved external symbol" error. I think I'm supposed to do something like this outside of the class declaration:

template<typename T>
T* (ClassTemplate<T>::*mFunctionPointer)() = NULL;

Unfortunately then I get C2998, "cannot be a template definition".

Any ideas?

+2  A: 

Change the position of the * so that it's

template<typename T>
T* (*ClassTemplate<T>::mFunctionPointer)() = NULL;

otherwise you are trying to define a namespace-level variable mFunctionPointer as a pointer-to-member of class ClassTemplate.

Troubadour
+1  A: 

Convert your definition to this:

template<typename T>
T* (*ClassTemplate<T>::mFunctionPointer)() = NULL;

The * should appear before the identifier (including class scope resolution).

Merlyn Morgan-Graham