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;
};