我正在尝试将二叉搜索树的内容写入临时数组,以便在main中使用。但是我不知道怎么做......我尝试过这样的事情:
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);
}
}
而且,它会出现以下错误:
将'temp'a声明为引用数组
'((Book *)this-> Book :: temp [x]'
中的'operator []'不匹配没有匹配函数来调用'Book :: preorder(TreeNode *&,Person&, INT&安培)'
答案 0 :(得分:2)
试试这个:
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);
}
}
答案 1 :(得分:2)
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);
}
}
是我的最终代码。而且我很确定它有效...以前的代码有某种逻辑错误。使用全局变量(我知道不推荐)我想出来了。