views:

45

answers:

2

I'm trying to write binary search tree's content to temporary array in order to use in main. However I'm not sure how to do it... I have tried something like this:

void Book::preorder(TreeNode *ptr, Person &temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}

And, it gives following errors:

declaration of 'temp'a as array of references

no match for 'operator[]' in '((Book*)this->Book::temp[x]'

no matching function for call to 'Book::preorder(TreeNode*&, Person&, int&)'

+1  A: 

Try this:

void Book::preorder(TreeNode *ptr, Person temp[], int x)
{

 if(ptr!=NULL)
 {
  temp[x].name=ptr->item.name;
  x++;
  preorder(ptr->left, temp, x);
  preorder(ptr->right, temp, x);
 }

}
CrociDB
Hmm.. No need to use pass by reference with arrays then?
woofeR
No. The way you were doing, you were passing an "array of references" and not a "reference of an array". But arrays don't need references, because they are pointers.
CrociDB
It worked. But I'm not sure it gives preorder traversal of list.
woofeR
A: 
void Book::preorder(TreeNode *ptr, Person temp[])
{
    if(ptr!=NULL)
    {
        temp[globX].name=ptr->item.name;
        temp[globX].phoneNumber=ptr->item.phoneNumber;
        globX++;
        preorder(ptr->left, temp);
        preorder(ptr->right, temp);
    }

}

is my final code. And i'm pretty sure that it works... Previous code has some kind of logic error. Using global variable (i know it's not recommended) I figured it out.

woofeR