4

I am getting this error in my AVL tree class as described in the title for this part of my code:

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);
}

Initially I thought I could just declare it as it is in the class, i.e. std::unique_ptr<TreeNode> rightRotate(std::unique_ptr<TreeNode>& y);

Anyone know what the problem is? Also, I am not sure if I should post more code of my class, trying to keep it minimal.

1 Answers1

7

Since the type AVL<T> depends on the template parameter T, if you want to refer to one of its member types, you need typename. So instead of std::unique_ptr<AVL<T>::TreeNode>, you must type std::unique_ptr<typename AVL<T>::TreeNode>.

A way to sidestep this issue is to use a trailing return type:

template <class T>
auto AVL<T>::rightRotate(std::unique_ptr<TreeNode>& y) -> std::unique_ptr<TreeNode> { /* ... */ }

Using the trailing return type forces the TreeNode in the return type to be looked up in the scope of AVL<T> just like it is in the parameter type.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312