使用 'typename' 关键字将非类型视为依赖上下文中的类型
Use the 'typename' keyword to treat nontype as a type in dependent context
我在我的 AVL 树 class 中收到此错误,如我这部分代码的标题中所述:
template <class T>
std::unique_ptr<AVL<T>::TreeNode> AVL<T>::rightRotate(std::unique_ptr<TreeNode>& y) {
std::unique_ptr<TreeNode> x = std::move(y->left);
std::unique_ptr<TreeNode> T2 = std::move(x->right);
// Perform rotation
x->right = std::move(y);
x->right->left = std::move(T2);
// Update heights
/ x->right->height = std::max(height(x->right->left), height(x->right->right)) + 1;
x->height = std::max(height(x->left), height(x->right)) + 1;
return std::move(x);
}
最初我以为我可以像在 class 中那样声明它,即 std::unique_ptr<TreeNode> rightRotate(std::unique_ptr<TreeNode>& y);
有人知道问题出在哪里吗?另外,我不确定我是否应该 post 我的 class 的更多代码,尽量减少它。
由于类型AVL<T>
依赖于模板参数T
,如果要引用其成员类型之一,则需要typename
。因此,您必须输入 std::unique_ptr<typename AVL<T>::TreeNode>
.
而不是 std::unique_ptr<AVL<T>::TreeNode>
回避此问题的一种方法是使用尾随 return 类型:
template <class T>
auto AVL<T>::rightRotate(std::unique_ptr<TreeNode>& y) -> std::unique_ptr<TreeNode> { /* ... */ }
使用尾部 return 类型强制 return 类型中的 TreeNode
在 AVL<T>
范围内查找,就像在参数类型中一样.
我在我的 AVL 树 class 中收到此错误,如我这部分代码的标题中所述:
template <class T>
std::unique_ptr<AVL<T>::TreeNode> AVL<T>::rightRotate(std::unique_ptr<TreeNode>& y) {
std::unique_ptr<TreeNode> x = std::move(y->left);
std::unique_ptr<TreeNode> T2 = std::move(x->right);
// Perform rotation
x->right = std::move(y);
x->right->left = std::move(T2);
// Update heights
/ x->right->height = std::max(height(x->right->left), height(x->right->right)) + 1;
x->height = std::max(height(x->left), height(x->right)) + 1;
return std::move(x);
}
最初我以为我可以像在 class 中那样声明它,即 std::unique_ptr<TreeNode> rightRotate(std::unique_ptr<TreeNode>& y);
有人知道问题出在哪里吗?另外,我不确定我是否应该 post 我的 class 的更多代码,尽量减少它。
由于类型AVL<T>
依赖于模板参数T
,如果要引用其成员类型之一,则需要typename
。因此,您必须输入 std::unique_ptr<typename AVL<T>::TreeNode>
.
std::unique_ptr<AVL<T>::TreeNode>
回避此问题的一种方法是使用尾随 return 类型:
template <class T>
auto AVL<T>::rightRotate(std::unique_ptr<TreeNode>& y) -> std::unique_ptr<TreeNode> { /* ... */ }
使用尾部 return 类型强制 return 类型中的 TreeNode
在 AVL<T>
范围内查找,就像在参数类型中一样.