C++ 非模板用作模板 - 嵌套模板 类
C++ Non-Template Used as Template - Nested Template Classes
我知道有人问过这方面的类似问题,但我似乎找不到解决我的确切问题的方法。我有一个 LinkedList
和 LinkedListIterator
class 结构如此。
template <typename T>
class LinkedList
{
public:
template <typename NodeType>
class LinkedListIterator
{
public:
const LinkedListIterator<NodeType>& operator++();
}
};
我正在尝试在单独的文件中实现 operator++()
函数,我的尝试如下。
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}
现在,当我尝试使用这个 class 时,我的编译器会抛出一个错误,指出 非模板“LinkedListIterator”用作模板 。现在我知道我必须在这个函数的定义中使用 typename
因为我有依赖范围发生。但我似乎无法解决这个问题。我不明白这是如何被视为 non-template
.
在 LinkedListIterator 之前添加 template 关键字,以便编译器知道它是一个模板而不是例如一个字段
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::template LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}
我知道有人问过这方面的类似问题,但我似乎找不到解决我的确切问题的方法。我有一个 LinkedList
和 LinkedListIterator
class 结构如此。
template <typename T>
class LinkedList
{
public:
template <typename NodeType>
class LinkedListIterator
{
public:
const LinkedListIterator<NodeType>& operator++();
}
};
我正在尝试在单独的文件中实现 operator++()
函数,我的尝试如下。
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}
现在,当我尝试使用这个 class 时,我的编译器会抛出一个错误,指出 非模板“LinkedListIterator”用作模板 。现在我知道我必须在这个函数的定义中使用 typename
因为我有依赖范围发生。但我似乎无法解决这个问题。我不明白这是如何被视为 non-template
.
在 LinkedListIterator 之前添加 template 关键字,以便编译器知道它是一个模板而不是例如一个字段
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::template LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}