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

标签 c++ templates static-members

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

#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|error: expected constructor, destructor, or type conversion before '*' token

最佳答案

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

关于c++ - 如何使用模板初始化类中的静态成员数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060287/

相关文章:

c++ - 关于windows 一个窗口的绘制周期

c++ - Boyer Moore - 坏字符规则实现子串搜索

c++ - 静态多态和模板容器

javascript - 非模板特定的帮助程序应该位于 Meteor 中的哪里?

c# - 局部变量与静态变量内存和性能

c++ - 有什么办法可以去掉 istream get 末尾的空字符?

尝试传递结构时 C++ 代码崩溃

java - 如何更改 IntelliJ 中的构造函数模板

c++ - 什么是静态方法?如何以及何时使用它们?

具有私有(private)静态成员的 C++ "undefined references"