C++ 模板类中的模板类-不同的参数

C++ 模板类中的模板类-不同的参数,c++,c++11,templates,iterator,C++,C++11,Templates,Iterator,是否可以像下面这样在模板类中创建模板类 // Container.h template <typename T> class Container { private: using iterator = Iterator<Node<T>>; using const_iterator = Iterator<const Node<T>>; // Node struct to hold data. template <t

是否可以像下面这样在模板类中创建模板类

// Container.h
template <typename T>
class Container
{
private:
  using iterator = Iterator<Node<T>>;
  using const_iterator = Iterator<const Node<T>>;

  // Node struct to hold data.
  template <typename T>
  struct Node
  {
    T data_;
  };

public:
  // Templated iterator for const or non-const.
  template <typename NodeType>
  class Iterator
  {
  private:
    NodeType* node_;
  
  public:
    Iterator();
  };
};

#include "Container.tpp"

这似乎不正确,因为我没有访问
节点类型的权限。任何建议都将不胜感激。

您应该将其定义为

template <typename T>        // for the enclosing class template
template <typename NodeType> // for the member template
Container<T>::Iterator<NodeType>::Iterator()
{
  // Implementation ...
}
template//用于封闭类模板
模板//用于成员模板
容器::迭代器::迭代器()
{
//实施。。。
}


顺便说一句:成员类模板
节点
不能使用与外部类模板相同的模板参数名称
t

您应该将其定义为

template <typename T>        // for the enclosing class template
template <typename NodeType> // for the member template
Container<T>::Iterator<NodeType>::Iterator()
{
  // Implementation ...
}
template//用于封闭类模板
模板//用于成员模板
容器::迭代器::迭代器()
{
//实施。。。
}


顺便说一句:成员类模板
节点
不能使用与外部类模板相同的模板参数名
t

Ok这对迭代器来说是有意义的。对于
节点
我是否应该删除该模板声明行?@nick2225如果希望它是模板本身,只需更改为使用另一个模板参数名(就像
迭代器
那样)。如果不必是模板,只需删除该行。@nick2225作为非模板类,
Node
也可以使用外部类模板的模板参数
t
。这对迭代器来说是有意义的。对于
节点
我是否应该删除该模板声明行?@nick2225如果希望它是模板本身,只需更改为使用另一个模板参数名(就像
迭代器
那样)。如果不必是模板,只需删除该行。@nick2225作为非模板类,
Node
也可以使用外部类模板的模板参数
t
。例如,“模板类中的模板类”意味着,
int
的容器需要
int
的节点,以及
char
的节点、
float
的节点、
std::vector
的节点等。这可能不是您的意思。也许你想重写你的问题来解释你想要什么,而不是试图通过代码来解释?@JaMiT我想songuanyao已经回答了。我想下一个有同样问题的人会因为问题的措辞而跳过阅读答案。例如,“模板类中的模板类”意味着,
int
的容器需要
int
的节点,以及
char
的节点、
float
的节点、
std::vector
的节点等。这可能不是您的意思。也许你想重写你的问题来解释你想要什么,而不是试图通过代码来解释?@JaMiT我想songuanyao已经回答了。我想下一个有同样问题的人会因为问题的措辞而跳过阅读答案。