关于用指向函数的指针作函数参数的process出错问题
#include <stdio.h>
void main()
{
int max(int a,int b);
int min(int a,int b);
int add(int a,int b);
int process(int a,int b, int (*fun)());
int x,y;
printf("input two numbers:\n");
scanf("%d %d",&x,&y);
printf("max is:%d\n",process(x,y,max));
printf("min is:%d\n",process(x,y,min));
printf("add is:%d\n",process(x,y,add));
}
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int add(int a,int b)
{
return a+b;
}
int process(int a,int b, int (*fun)())
{
int z= (*fun)(a,b);
return z;
}
--------------------Configuration: 01 - Win32 Debug--------------------
Compiling...
01.cpp
d:\program files\microsoft visual studio\myprojects\20160608\01.cpp(15) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
d:\program files\microsoft visual studio\myprojects\20160608\01.cpp(16) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
d:\program files\microsoft visual studio\myprojects\20160608\01.cpp(17) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
None of the functions with this name in scope match the target type
d:\program files\microsoft visual studio\myprojects\20160608\01.cpp(41) : error C2197: 'int (__cdecl *)(void)' : too many actual parameters
执行 cl.exe 时出错.
01.obj - 1 error(s), 0 warning(s)
重新创建*.c文档试试。 定义跟赋值分开 #include <stdio.h>
void main()
{
int max(int a,int b);
int min(int a,int b);
int add(int a,int b);
int process(int a,int b, int (*fun)(int, int));
int x,y;
printf("input two numbers:\n");
scanf("%d %d",&x,&y);
printf("max is:%d\n",process(x,y,max));
printf("min is:%d\n",process(x,y,min));
printf("add is:%d\n",process(x,y,add));
}
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int add(int a,int b)
{
return a+b;
}
int process(int a,int b, int (*fun)(int, int))
{
int z= (*fun)(a,b);
return z;
}
{:5_98:}{:5_98:} 大兄弟 你的程序提示的 错误不能传送参数从 (int,int)到(void)
所以你要把 process的(*fun)()改为(*fun)(int,int)这个在教程中都有说的 ,,要认真学习哦
共同学习 !!!!!!!!!!!!!!!!!!!!!!!!!1
页:
[1]