【问题标题】:struct of template class in template class [duplicate]模板类中模板类的结构[重复]
【发布时间】:2016-02-25 08:34:15
【问题描述】:

告诉我为什么不能在另一个模板类中使用模板类的结构。 我个人认为这是合乎逻辑的。有c++这样的功能吗? 我正在使用 VS 2015。谢谢:)

template<typename T> class MyList
{
public:
    struct Node
    {
        T       value;
        Node*   next;
    };
    //...
};

template<typename Type> class MyMap
{
public:
    struct ElementData
    {
        Type        types[32];
        unsigned    key;
    };

    MyList<ElementData>::Node* nodes;   //Syntax Error: Identifier 'Node'
};

虽然这有效。

template <typename T> struct Node
{
    T       value;
    Node*   next;
};

template<typename T> class MyList
{
public:
    Node<T>* root;
    //...
};

template<typename Type> class MyMap
{
public:
    struct ElementData
    {
        Type        types[32];
        unsigned    key;
    };

    Node<ElementData>* nodes;   
};

【问题讨论】:

  • 你知道类型名吗?

标签: c++ templates c++11


【解决方案1】:

您需要在此处添加typename(用于依赖类型名称MyList&lt;ElementData&gt;::Node),

typename MyList<ElementData>::Node* nodes;

在模板的声明或定义中,typename 可用于声明从属名称是一种类型。

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

【讨论】:

  • 现在可以通过“decltype”实现吗?
  • @dahohu527 我不这么认为,decltype 需要一个表达式来获取类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多