如何使用模板初始化类中的静态成员数组

How to initialize static member array in class with template

本文关键字:静态成员 数组 初始化 何使用      更新时间:2023-10-16

可能的重复项:
我必须在哪里以及为什么必须放置"模板"和"类型名称"关键字?

#include <iostream>
using namespace std;
template <class T>
class Test
{
    union obj
    {
        union obj* next;
        int num;
    };
    static const int SZ=3;
    static obj* volatile list[SZ];
};
template <class T>
Test<T>::obj* volatile
Test<T>::list[SZ]=
{
    0, 0, 0
};
int main()
{
    return 0;
}

使用g ++,我得到的错误是:

18|错误:在"*"标记之前进行预期的构造函数、析构函数或类型转换

成员定义中Test<T>::obj*之前添加关键字 typename。