鱼C论坛

 找回密码
 立即注册
查看: 1377|回复: 5

[已解决]process哪里出问题了!

[复制链接]
发表于 2018-8-27 15:53:12 | 显示全部楼层 |阅读模式

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

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

x
/*******************************************************/
/*设一个函数process,在调用它的时候,每次实现不同的功能*/
/*输入a和b两个数,第一次调用process时找出a和b中大者,第*/
/*二次找出其中小者,第三次求a与b之和。                 */
/*******************************************************/
#include <stdio.h>

void main()
{
        int max(int, int);
        int min(int, int);
        int add(int, int);

        void process(int x, int y, int(*fun)());

        int a, b;
        printf("Endter a and b:");
        scanf("%d %d", &a, &b);
        printf("max = ");
        process(a, b, max);
        printf("min = ");
        process(a, b, min);
        printf("sun = ");
        process(a, b, add);

}
int max(int x, int y)
{
        int z;
        if(x > y)
        {
                z = x;
        }
        else
        {
                z = y;
        }
        return z;
}
int min(int x, int y)
{
        int z;
        if(x < y)
        {
                z = x;               
        }
        else
        {
                z = y;
        }
        return z;
}
int add(int x, int y)
{
        int z;
        z = x + y;
        return z;
}
void process( int x, int y, int(*fun)() )
{
      int result;
      result = (*fun)(x,y);
      printf("%d\n", result);
}

错误指令:
E:\编程\C例子\26process.cpp(20) : 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
E:\编程\C例子\26process.cpp(22) : 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
E:\编程\C例子\26process.cpp(24) : 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
E:\编程\C例子\26process.cpp(62) : error C2197: 'int (__cdecl *)(void)' : too many actual parameters
最佳答案
2018-8-27 17:32:58
本帖最后由 claws0n 于 2018-8-27 17:36 编辑

楼主,修改了,.cpp 也适用。函数的声明最好是在 main() 外面
#include <stdio.h>

int max(int, int);
int min(int, int);
int add(int, int);
void process(int x, int y, int(*fun)(int, int));  // 带参数的函数

int main()
{
        int a, b;
        printf("Enter a and b:");
        scanf_s("%d %d", &a, &b);
        printf("max = ");
        process(a, b, max);
        printf("min = ");
        process(a, b, min);
        printf("sum = ");
        process(a, b, add);

        return 0;
}

int max(int x, int y)
{
        if (x > y)
        {
                return x;
        }
        else
        {
                return y;
        }
}

int min(int x, int y)
{
        if (x < y)
        {
                return x;
        }
        else
        {
                return y;
        }
}

int add(int x, int y)
{
        return x + y;
}

void process(int x, int y, int(*fun)(int, int))  // 带参数的函数
{
        printf("%d\n", (*fun)(x, y));  // 带参数的函数
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-27 16:42:58 | 显示全部楼层
我可以编译,没问题。(clang和gcc都成功编译)
要不试试把函数声明加到前面去?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-27 16:46:32 | 显示全部楼层
VS?档名请确定是 .c 不是 .cpp
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-27 17:32:58 | 显示全部楼层    本楼为最佳答案   
本帖最后由 claws0n 于 2018-8-27 17:36 编辑

楼主,修改了,.cpp 也适用。函数的声明最好是在 main() 外面
#include <stdio.h>

int max(int, int);
int min(int, int);
int add(int, int);
void process(int x, int y, int(*fun)(int, int));  // 带参数的函数

int main()
{
        int a, b;
        printf("Enter a and b:");
        scanf_s("%d %d", &a, &b);
        printf("max = ");
        process(a, b, max);
        printf("min = ");
        process(a, b, min);
        printf("sum = ");
        process(a, b, add);

        return 0;
}

int max(int x, int y)
{
        if (x > y)
        {
                return x;
        }
        else
        {
                return y;
        }
}

int min(int x, int y)
{
        if (x < y)
        {
                return x;
        }
        else
        {
                return y;
        }
}

int add(int x, int y)
{
        return x + y;
}

void process(int x, int y, int(*fun)(int, int))  // 带参数的函数
{
        printf("%d\n", (*fun)(x, y));  // 带参数的函数
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-29 07:40:24 | 显示全部楼层
跟楼上的观点相反,跟放在哪里的,编译没有关系!
在函数声明时,你要声明的传入的数据的类型跟输入的数据类型是一样!
void process(int x, int y, int(*fun)(int, int));

如上*(fun),要加入(int,int),去声明传入数据的类型
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-8 10:04:37 | 显示全部楼层
#include <stdio.h>

int  main()
{
        int max(int, int);
        int min(int, int);
        int add(int, int);

        void process(int x, int y, int(*fun)(int,int));

        int a, b;
        printf("Endter a and b:");
        scanf("%d %d", &a, &b);
        printf("max = ");
        process(a, b, max);
        printf("min = ");
        process(a, b, min);
        printf("sun = ");
        process(a, b, add);
        return 0;

}
int max(int x, int y)
{
        int z;
        if(x > y)
        {
                z = x;
        }
        else
        {
                z = y;
        }
        return z;
}
int min(int x, int y)
{
        int z;
        if(x < y)
        {
                z = x;               
        }
        else
        {
                z = y;
        }
        return z;
}
int add(int x, int y)
{
        int z;
        z = x + y;
        return z;
}
void process( int x, int y, int(*fun)(int,int) )
{
      int result;
      result = (*fun)(x,y);
      printf("%d\n", result);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 14:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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