简体   繁体   English

类模板中的静态函数指针成员

[英]Static function pointer member in a class template

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: 我正在Visual Studio中使用C ++,并且我的代码类似于以下内容:

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". 不幸的是,然后我得到C2998,“不能是模板定义”。

Any ideas? 有任何想法吗?

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 . 否则,您尝试将名称空间级别的变量mFunctionPointer定义为指向mFunctionPointer的成员的ClassTemplate

Convert your definition to this: 将您的定义转换为此:

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

The * should appear before the identifier (including class scope resolution). *应该出现在标识符之前(包括类范围解析)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM