0

I decided to make class for AVL tree. I created insertion method in AVL class but I get error from CLion. It says: Invalid use of 'this' outside of a non-static member function. this is my code:

#include <iostream>
using namespace std;
//declaration of the class called AVL
template <typename T>
class AVL{
public:
//    declaration of variables
    AVL <T> *left = NULL,*right = NULL;
    T key;
    int size = 0;
    int height = 0;
//define the function for insertion of new element in AVL tree
    void insert(T value, AVL <T>* &current = this, AVL <T>* &root = this){
//        if size of AVL tree is 0 then just insert new element and return
        if(root->size == 0){
            root->size++;
            current->key = value;
            return;
        }
        if(current == NULL){
//            creation of new node
            root->size++;
            current = new AVL<T>;
            current -> key = value;
            return;
        }
        if(value < current->key){
//            go to the left child
            current->height--;
            insert(value,current->left,root);
        }
        else{
//            go to the right child
            current->height++;
            insert(value,current->right,root);
        }
    }
};
int main() {
//    creation of the class instance
    AVL<long long>X;
}

in the following code fragment:

AVL <T>* &current = this

'this' is underlined with red line. Can someone explain me why I get this error.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 5
    Does this answer your question? [Not possible: this pointer as a default argument. Why?](https://stackoverflow.com/questions/12828216/not-possible-this-pointer-as-a-default-argument-why) – GoodDeeds Jan 20 '20 at 11:58
  • `this` is always passed to every non-static member function as the first parameter. Why would you pass it again? Just `nullptr` last argument as default. – Hawky Jan 20 '20 at 11:58
  • @Hawky Because `nullptr` might have a different meaning? This code even shows exactly that... – Max Langhof Jan 20 '20 at 12:02
  • 1
    The straightforward solution to this problem is to create an overload (or a differently named function) that calls the "complicated" function with the default parameters. For example, rename `insert` to `insert_impl` and remove its default parameters, then add `void insert(T value) { insert_impl(value, this, this); }`. – Max Langhof Jan 20 '20 at 12:04
  • 1
    not covered in the dupe: There are several things that cannot be used as default argument, though you can always write an overload for the function that passes the parameter to the original method (with no defaults) – 463035818_is_not_an_ai Jan 20 '20 at 12:10
  • @formerlyknownas_463035818 I considered adding such an answer to the dupe, but the question there kind of covers that already... – Max Langhof Jan 20 '20 at 12:14
  • @MaxLanghof and formerlyknownas_463035818 thanks for the solution. Although I think that the code will be more ugly :D. My code might be more beautiful if I make AVL Tree using structs for tree nodes. – Sandro Skhirtladze Jan 20 '20 at 12:19
  • @MaxLanghof somehow I overlooked your comment, but I didnt find it in the dupe.... nevermind, OP got their answer I think – 463035818_is_not_an_ai Jan 20 '20 at 12:40

0 Answers0