鱼C论坛

 找回密码
 立即注册
查看: 1579|回复: 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() 外面
  1. #include <stdio.h>

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

  6. int main()
  7. {
  8.         int a, b;
  9.         printf("Enter a and b:");
  10.         scanf_s("%d %d", &a, &b);
  11.         printf("max = ");
  12.         process(a, b, max);
  13.         printf("min = ");
  14.         process(a, b, min);
  15.         printf("sum = ");
  16.         process(a, b, add);

  17.         return 0;
  18. }

  19. int max(int x, int y)
  20. {
  21.         if (x > y)
  22.         {
  23.                 return x;
  24.         }
  25.         else
  26.         {
  27.                 return y;
  28.         }
  29. }

  30. int min(int x, int y)
  31. {
  32.         if (x < y)
  33.         {
  34.                 return x;
  35.         }
  36.         else
  37.         {
  38.                 return y;
  39.         }
  40. }

  41. int add(int x, int y)
  42. {
  43.         return x + y;
  44. }

  45. void process(int x, int y, int(*fun)(int, int))  // 带参数的函数
  46. {
  47.         printf("%d\n", (*fun)(x, y));  // 带参数的函数
  48. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

发表于 2018-8-27 16:46:32 | 显示全部楼层
VS?档名请确定是 .c 不是 .cpp
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

  6. int main()
  7. {
  8.         int a, b;
  9.         printf("Enter a and b:");
  10.         scanf_s("%d %d", &a, &b);
  11.         printf("max = ");
  12.         process(a, b, max);
  13.         printf("min = ");
  14.         process(a, b, min);
  15.         printf("sum = ");
  16.         process(a, b, add);

  17.         return 0;
  18. }

  19. int max(int x, int y)
  20. {
  21.         if (x > y)
  22.         {
  23.                 return x;
  24.         }
  25.         else
  26.         {
  27.                 return y;
  28.         }
  29. }

  30. int min(int x, int y)
  31. {
  32.         if (x < y)
  33.         {
  34.                 return x;
  35.         }
  36.         else
  37.         {
  38.                 return y;
  39.         }
  40. }

  41. int add(int x, int y)
  42. {
  43.         return x + y;
  44. }

  45. void process(int x, int y, int(*fun)(int, int))  // 带参数的函数
  46. {
  47.         printf("%d\n", (*fun)(x, y));  // 带参数的函数
  48. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

如上*(fun),要加入(int,int),去声明传入数据的类型
小甲鱼最新课程 -> https://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);
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 17:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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