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

时间:2013-04-06 01:42:50

标签: c++ templates static-initialization

我有类似的东西

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

1 个答案:

答案 0 :(得分:1)

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++ difference of keywords 'typename' and 'class' in templates

  2. 为什么会这样,请参阅Trouble with dependent types in templates

相关问题