Home > Mobile >  "error: cannot convert 'int*' to 'int**'"
"error: cannot convert 'int*' to 'int**'"

Time:05-02

I try to implement a template avl tree while Node includes a pointer to the object :

template <class T>
class Node {
public:
    Node* left;
    T* data;
    Node* right;
    int height;
};
    
template <class T>
class AVLTree{
public:
    Node<T*>* root;
  
Node<T*>* insert(Node<T*>* p, T* key){
    Node<T*>* t;
    if (p == NULL){
        t = new Node<T*>;
        t->data = key;
        t->left = NULL;
        t->right = NULL;
        t->height = 1;  
        return t;
    }
 
    if (*(key) < *(p->data)){
        p->left = insert(p->left, key);
    } else if (*(key) > *(p->data)){
        p->right = insert(p->right, key);
    }
 


    return p;
}
int main() {
    
    AVLTree<int*>* tree1 = new AVLTree<int*>();

    int a=5;
    int* ap=&a;

   
tree1->root = tree1->insert(tree1->root,ap);  

By executing this I got an error, I hope there are enough details about the problem.

enter image description here

please help . thanks !

CodePudding user response:

T = int* and you say that you want a T* (that is, an int**) in T* key so when you try to supply an int* instead, it fails.

I suggest changing the Node to contain a T instead of a T*:

template <class T>
class Node {
   public:
    Node* left;
    T data;       // not T*
    Node* right;
    int height;
};
template <class T>
class AVLTree {
public:
    Node<T>* root;

    Node<T>* insert(Node<T>* p, T* key) {
        Node<T>* t;
        if (p == nullptr) {
            t = new Node<T>;
            t->data = *key;
            t->left = nullptr;
            t->right = nullptr;
            t->height = 1;
            return t;
        }

        if (*key < p->data) {
            p->left = insert(p->left, key);
        } else if (*key > p->data) {
            p->right = insert(p->right, key);
        }

        return p;
    }
};

And then instantiate an AVLTree<int> instead of an AVLTree<int*>


Here's a suggestion making it possible to store pointers and comparing them using std::less and std::greater. Note that if you store pointers, it'll be the actual pointer values that you compare, not the values the pointers point at.

#include <functional>

template <class T>
class Node {
   public:
    Node* left;
    T data;
    Node* right;
    int height;
};

template <class T>
class AVLTree {
public:
    Node<T>* root;

    Node<T>* insert(Node<T>* p, const T& key) { // key is an int*& in this example
        Node<T>* t;
        if (p == nullptr) {
            t = new Node<T>;
            t->data = key;
            t->left = nullptr;
            t->right = nullptr;
            t->height = 1;
            return t;
        }

        if (std::less<T>{}(key, p->data)) {           // less
            p->left = insert(p->left, key);
        } else if (std::greater<T>{}(key, p->data)) { // greater
            p->right = insert(p->right, key);          
        }

        return p;
    }
};

int main() {
    AVLTree<int*> tree1;

    int a = 5;
    int* ap = &a;

    tree1.root = tree1.insert(tree1.root, ap);
}
  •  Tags:  
  • c
  • Related