2014-05-11 47 views
1

我一直在试图编写一个函数来打印一个二叉树的内容,其中最左边的分支最靠近A,最靠近Z的最右边。 但是由于我写了这个程序,我有两个互相调用的函数,这个函数会导致一个被隐式定义,是否有更简单的方法来做到这一点〜? /我可以修复它吗?尝试打印二叉树内容时的函数声明

/* print() */ 
static void print (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer) 
{ 
fprintf (stderr , "print called"); 
if (print_pointer->printed == 0) 
{ 
printf ("\nTitle: %s\n" , print_pointer->title); 
printf ("Author: %s\n" , print_pointer->author); 
printf ("Year: %i\n" , print_pointer->year); 
print_pointer->printed = 1; 
np++; 
print_pointer = book_tree; 
} 
} 

/* descend_right() */ 
void descend_right (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer) 
{ 
fprintf (stderr , "descend_right called"); 
if (print_pointer->right->printed == 0) 
{ 
print_pointer = print_pointer->right; 
descend_left(title , author , year , printed , np , print_pointer); 
} 
if (print_pointer->right == NULL) 
{ 
print(title , author , year , printed , np , print_pointer); 
} 
if (print_pointer->right->printed == 1) 
{ 
descend_right(title , author , year , printed , np , print_pointer); 
} 
} 

/* descend_left() */ 
void descend_left (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer) 
{ 
fprintf (stderr , "descend_left called"); 
if (print_pointer->left->printed == 0) 
{ 
print_pointer = print_pointer->left; 
descend_left(title , author , year , printed , np , print_pointer); 
} 
if (print_pointer->left == NULL) 
{ 
print(title , author , year , printed , np , print_pointer); 
descend_right(title , author , year , printed , np , print_pointer); 
} 
if (print_pointer->left->printed == 1) 
{ 
print(title , author , year , printed , np , print_pointer); 
descend_right(title , author , year , printed , np , print_pointer); 
} 
} 

/* menu_print_database(): */ 
static void menu_print_database (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int bn) 
{ 
struct Book *print_pointer = book_tree; 
int np = 0; 
do 
{ 
if (book_tree->left->printed == 1 && book_tree->right->printed == 0) print(title , author , year , printed , np , print_pointer); 
if (print_pointer->printed == 0) descend_left (title , author , year , printed , np , print_pointer); 
if (print_pointer->printed == 1) descend_right (title , author , year , printed , np , print_pointer); 
} 
while (np != bn); 
} 

回答

1

您需要添加的功能之一的前向声明,就像这样:

static void print (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer); 
void descend_right (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer); 
void descend_left (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer); 

此作为承诺编译器中的代码一段时间后,确定这些功能你程序。它告诉编译器这些函数的名称,它们的返回类型以及它们的参数类型 - 它需要知道产生该函数调用的所有信息。

在第一个函数之前添加这三行来解决问题。