无法将参数从'this'转换为Node <t> *

时间:2019-01-16 14:52:47

标签: c++

我正在研究一个新项目,以更好地了解模板。 我在转换扩充时遇到问题;我不确定如何解决此问题,并想了解这一点。 这是二进制搜索树上的一个项目。

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'节点* Tree :: insertion(节点*,T)':无法转换   参数1从'Tree * const'到'Node *'

1 个答案:

答案 0 :(得分:1)

如消息所示,您正在将this(即Tree<T>* const)传递给insertion,而后者期望Node<T>*

您可能是说root = insertion(root, value);

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

相关问题