Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Msg: cannot convert parameter 1 from 'Node *' to 'Node'

I'm writing an expression tree.

The Node class has instances of itself as members left, right and parent.

Thanks to James McNellis in this post, I declared them as pointers.

   class Node
   {
     public:
        char *cargo; 
        int depth; 
        Node *parent;
        Node *left; 
        Node *right;
    //constructors/destructor:
        Node(void); 
        Node(int a_depth, Node *pparent = __nullptr); 
        ~Node();
    //method:
        void traverse_tree(Node n)
    };

Now I try to traverse the tree and print it out (to file "out").

Recursively calling 'traverse_tree(left);' and 'traverse_tree(right);'

causes the error message "cannot convert parameter 1 from 'Node *' to 'Node'".

Traverse_tree is called initially with the root node as the arguement.

I think the declaration of the parameter "(Node n)" is confusing the compiler and it doesn't know

whether to call a constructor or not.

How do I pass "left" and "right" to the "traverse_tree" method?

void Node::traverse_tree(Node n)
    //utility to view the tree
{
    if (((left) ==  __nullptr)||((right) ==  __nullptr))
    {
        return;
    }
    traverse_tree(right);
    out<<'  '<<n.cargo<<"\n";
    traverse_tree(left);
    return;
};
like image 265
Peter Stewart Avatar asked Mar 23 '26 11:03

Peter Stewart


1 Answers

Dereference your pointers:

traverse_tree(*right);

You might also want to change your traverse_tree method to accept a reference:

void traverse_tree(Node &n)
like image 170
Mark Byers Avatar answered Mar 26 '26 01:03

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!