简体   繁体   English

类模板的静态成员初始化

[英]Static Member Initialization of class template

Please find the code below: 请在下面找到代码:

#include <iostream>
using namespace std;
template<typename T>
class A
{
    static A* self;
};

template<typename T>
A* A<T>::self = NULL;
int main()
{
    return 0;
}

I am facing a compilation problem while initialising the static pointer. 我在初始化静态指针时遇到编译问题。 Even though so many links has said the same way (the way i initialised) but still the problem resist. 即使如此多的链接以相同的方式(我初始化的方式)说了,但问题仍然存在。 Below is the compilation error. 下面是编译错误。

"expected constructor, destructor, or type conversion before '*' token"

A is a class template, so you need to specify the template parameter when defining the pointer to it. A是一个类模板,因此在定义指向它的指针时需要指定template参数。

Instead of: 代替:

template<typename T>
A* A<T>::self = NULL;

it should be: 它应该是:

template<typename T>
A<T>* A<T>::self = NULL;

Within the class body, specifying the template parameter is optional, so you can write A* there and it will be treated the same as A<T> * . 在类主体中,指定template参数是可选的,因此您可以在其中写入A* ,并将其与A<T> *

尝试:

template<typename T> A<T>* A<T>::self = NULL;

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

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