Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code to a set of methods for a binary tree and have received this compilation error:

pfbst.c: In function ‘main’:
pfbst.c:65:13: warning: passing argument 2 of ‘bst_char_iterate’ from incompatible pointer type [enabled by default]
bst_char_iterate(t,print_char);
^
In file included from pfbst.c:6: 0:
pfbst_char.h:26:6: note: expected ‘char (*)(char)’ but argument is of type ‘void (*)(char)’
int bst_char_iterate(bst_char *t, char (*fun)(char item));
^


Here's the code:

C
int bst_iterate(struct node *p, char (*fun)(char item)) {
    if (p!=NULL) {
       p->item = fun(p->item);
       bst_iterate(p->left,fun);
       bst_iterate(p->right,fun);
    } else {
       return 0;
    }
}

int bst_char_iterate(bst_char *t, char (*fun)(char item)) {
    assert(t!=NULL);
    bst_iterate(t->root,fun);
}
Posted
Comments
CPallini 13-Jan-14 15:53pm    
The error is in your main function, for better help, you should post the relevant code here.
CHill60 13-Jan-14 15:56pm    
Beat me to it!

1 solution

C++
bst_char_iterate(t,print_char); 

part causes the error. Your print_char function signature differs from the expected one as in the error.
Quote:
pfbst_char.h:26:6: note: expected ‘char (*)(char)’ but argument is of type ‘void (*)(char)’

If you change print_char's return type to char and return a char in the function, the compiler will stop complaining :)
C++
char print_char(char c) {
 return 0;
}
 
Share this answer
 
Comments
Stefan_Lang 14-Jan-14 10:47am    
Good work - looks like a perfect fix for me, constructed from only the error message without the code! My 5!

On a sidenote: this should be a lesson to the poster: just reading the error message reveals the problem and the fix!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900