无法将'this'指针传递给类方法参数作为C ++中的默认变量

时间:2020-01-20 11:54:41

标签: c++ class methods

我决定参加AVL树课程。我在AVL类中创建了插入方法,但是从CLion中收到错误消息。它说:在非静态成员函数外部无效使用“ this”。 这是我的代码:

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

在以下代码片段中:

AVL <T>* &current = this

“此” 用红线下划线。有人可以解释一下为什么我会收到此错误。

0 个答案:

没有答案
相关问题