如何用内部类的类型初始化模板类中的静态字段

How to initialize static field in template class with type of inner class

本文关键字:静态 字段 初始化 何用 内部类 类型      更新时间:2023-10-16

我有这样的东西

template <class T>
class Outer {
    public: class Inner;
    static Inner* x;
    //...
    class Inner {
        //...
    };
};
// Not working
template <class T>
Outer<T>::Inner* Outer<T>::x = NULL;

我得到的错误说::16: error: expected constructor, destructor, or type conversion before ‘*’ token

template<class T>
class Outer {
public: 
    class Inner;
    static Inner* x;
    //...
    class Inner {
        //...
    };
};
template<class T>
typename Outer<T>::Inner *Outer<T>::x = NULL;
  1. 对于typenameclass,请参考c++中keywords 'typename'和& # 39;类# 39;在模板

  2. 为什么会这样,请参考模板中依赖类型的问题