Take the 2-minute tour ×

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
        char *cargo; 
        int depth; 
        Node *parent;
        Node *left; 
        Node *right;
        Node(int a_depth, Node *pparent = __nullptr); 
        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))
    out<<'  '<<n.cargo<<"\n";
share|improve this question

2 Answers 2

up vote 2 down vote accepted

I think you should be calling traverse_tree with a node pointer, not a node. You generally use pointers for this sort of operation. This would result in something like:

void Node::traverse_tree (Node *n) {
    if ((left == __nullptr) || (right == __nullptr))
    traverse_tree (right);
    out << "  " << n.cargo << "\n";
    traverse_tree (left);

and you would call it with:

root.traverse_tree (&root);

You may want to re-engineer your code at some point to make it more C++-like:

void Node::traverse_tree(void) {
    out << "  " << cargo << "\n";

In other words, traverse using the methods of the sub-nodes themselves rather than passing the pointers (which is really the "old-school" non-object-oriented way of doing things).

share|improve this answer
The scales fell from my eyes! Of course. I'm keeping the behaviour with the data.' In the fourth line from the end I removed the n. from n.cargo, and I added another "out<<' '<<n.cargo<<"\n"; " before the first return so that the leaf nodes would be displayed. It works except it returns 8224 before every cargo, like this 82241.0 8224* 82249.0 8224- 82241.0 .... I'm calling it with no arguements: root.traverse_tree(); Thanks for the lesson! –  Peter Stewart Jun 20 '10 at 18:57
8224 problem solved. Single quotes changed to double quotes in " out<<" "<<cargo<<"\n";" –  Peter Stewart Jun 20 '10 at 20:59
@Peter, the 8224 problem is because ' ' is a multi-byte character constant which turns into an integer. The two spaces (each hex 20 or decimal 32) give you the constant 32 * 256 + 32 = 8192 + 32 = 8224. –  paxdiablo Jun 21 '10 at 8:42

Dereference your pointers:


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

void traverse_tree(Node &n)
share|improve this answer
This works. Thanks, I'll wait a while before accepting an answer. –  Peter Stewart Jun 20 '10 at 14:48
If I de-reference the pointer, why would the method expect a reference? –  Peter Stewart Jun 20 '10 at 19:28
although, obviously it does, as this solved the problem. Thanks –  Peter Stewart Jun 21 '10 at 11:10

Your Answer


