如何解决模板类内部的模板类-不同的参数
是否可以在如下所示的模板类中创建模板类
// 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"
因此,我在这里声明一个迭代器的模板,该模板采用一个NodeType
,它不同于容器类模板采用的T
。
如果可能的话,如何在另一个文件中实现Iterator()
?像
// Container.tpp
template <typename T>
LinkedList<T>::LinkedListIterator<NodeType>::Iterator()
{
// Implementation ...
}
这似乎不正确,因为我无权访问NodeType
。任何建议将不胜感激。
解决方法
您应该将其定义为
template <typename T> // for the enclosing class template
template <typename NodeType> // for the member template
Container<T>::Iterator<NodeType>::Iterator()
{
// Implementation ...
}
顺便说一句:成员类模板Node
不能使用与外部类模板相同的模板参数名称T
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。