鱼C论坛

 找回密码
 立即注册
查看: 2891|回复: 4

关于用指向函数的指针作函数参数的process出错问题

[复制链接]
发表于 2016-6-8 11:20:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x



#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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-6-8 14:13:13 | 显示全部楼层
重新创建*.c文档试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-8 14:33:14 From FishC Mobile | 显示全部楼层
定义跟赋值分开
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-8 15:53:11 | 显示全部楼层
#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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-8 22:10:33 | 显示全部楼层
    大兄弟   你的程序提示的 错误  不能传送参数从 (int,int)到(void)
     所以你要把 process的(*fun)()改为(*fun)(int,int)  这个在教程中都有说的 ,,要认真学习哦  
             共同学习 !!!!!!!!!!!!!!!!!!!!!!!!!1
360截图20160608220735036.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-15 20:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表