无法将参数从"this"转换为节点<T>*

Cannot convert argument from 'this' to Node<T>*

本文关键字:lt gt 节点 转换 参数 this      更新时间:2023-10-16

我正在做一个新项目,以更好地理解模板。我在转换增强时遇到问题;我不确定如何处理这个问题,想了解一下。这是一个关于二叉搜索树的项目。

template <typename T>
Node<T>* Tree<T>::insertion(Node<T>* root , T value){
    if (this == NULL) {
        Node n = new Node(value);
        return n;
    }
    if (value < this->value)
        this->left = insert(this->left, value);
    else if (value > this->value)
        this->right = insert(this->right, value);
    return root;
}
template <typename T>
void Tree<T>::insert(T value) {
    insertion(this, value);
}

类树:

class Tree {
private:
    Node<T>* root;
public:
    Tree() {
        this->root = NULL;
    };
    ~Tree() { recursiveDeletion(this->root);  }
    void insert(T value);
    int find(T value) const;
    //int size() const;
    friend ostream& operator<<(ostream &b, Tree const &t);
    Node<T>* insertion(Node<T> *root, T value);
};

类节点:

template <typename T>
class Node {
private:
    Node *right, *left;
    T value;
public:
    Node() {
        right = NULL = left;
    }
    Node(T value) { 
        this->value = value;
        right = NULL = left;
    };
};

主要:

void main()
{
    Tree<int> *root = new Tree<int>();
    root->insert(1);
    root->insert(2);
    root->insert(-2);
    root->insert(-1);
    root->insert(2);
    root->find(-1);
    root->size();
    cout << root << endl;
}

错误 C2664"节点*树::插入(节点*,T)":无法转换 参数 1 从"树 *常量"到"节点 *"

正如消息所说,您正在将this(这是一个Tree<T>* const)传递给insertion,后者期望Node<T>*

你可能的意思是root = insertion(root, value);.

(该代码还有无数其他问题,但这是该特定错误消息的来源。