error C2664: cannot convert parameter 1 from 'int' to 'int (__cdecl *)(int)'

tuk picture tuk · Jun 13, 2011 · Viewed 13.2k times · Source

having some trouble passing a function as a parameter of another function...

ERROR: Error 1 error C2664: 'wrapper' : cannot convert parameter 1 from 'int' to 'int (__cdecl *)(int)'

int inc( int n )
{
    return n + 1 ;
}

int dec( int n )
{
    return n - 1 ;
}

int wrapper(   int i, int func(int)   )
{
    return func( i ) ;
}   


int main(){

    int a = 0 ;

    a = wrapper(  3, inc( 3 )  ) ;

    return 0 ;

}

Answer

Mark B picture Mark B · Jun 13, 2011

You're passing the result of a function call inc(3) to wrapper, NOT a function pointer as it expects.

a = wrapper(3, &inc) ;