C++: no matching function call

74 Views Asked by At

I am getting the above error and its pointing to my call for add(root, node). What does this error mean? I have tried moving the private add to the top but that doesnt work either. Also, it cant be because its private because its in the same class, right?

class Tree{

    public:
        Node *root;
        Tree(Node *r){
            root = r;
        }

        void add(Node node){
            add(root, node);//error here
        }
    private:
        void add(Node parent, Node node){

            if(parent == root && root == nullptr){
                root = node;
            }

            if(parent == nullptr){
                parent(node->value, nullptr, nullptr);
            }
            else if(node > parent){
                add(parent->right, node);
            }
            else {
                add(parent->left, node);
            }
2

There are 2 best solutions below

0
On

Your function needs the signature

void add(Node* parent, Node* node)

Note that these are Node* instead of Node. The same goes for the public overload of that function

void add(Node* node)

This is apparent because 1) you are doing comparisons to nullptr and 2) you keep dereferencing your variables with -> instead of .

0
On

CoryKramer has spotted some errors, I'll add this also.

you can't compare pointers so it should be data:

else if(node->value > parent->value){
            add(parent->right, node);