请教C语言高手们
请问高手们,这个程序为什么出现“Type mismatch in redeclaration of 'calculator '”错误信息,这个程序是那个地方出错了呀?
main()
{//声明函数
float add(float a,float b);
float sub(float a,float b);
float mul(float a,float b);
float div(float a,float b);
float x,y;//定义两个实型变量
printf("input x and y:");
scanf("%f,%f",&x,&y);//输入两个数
printf("x+y=");//求两个数的和
calculator(x,y,add);
printf("x-y=");//求两个数的差
calculator(x,y,sub);
printf("x*y=");//求两个数的积
calculator(x,y,mul);
printf("x/y=");//求两个数的商
calculator(x,y,div);
}
float add(float a,float b)//定义求和函数
{return (a+b);}
float sub(float a,float b)//定义求差函数
{return (a-b);}
float mul(float a,float b)//定义求积函数
{return (a*b);}
float div(float a,float b)//定义求商函数
{return (a/b);}
//定义调用以上四个函数中某个函数的函数
calculator(float a, float b, float (*pfunc)(float, float))
{
float result;
result = (*pfunc)(a, b);//用指向函数的指针调用函数
printf("%f\n",result);
}
[解决办法]
calculator也是个函数
那你为什么没有声明呢?
还有calculator既然是无返回值
其定义应该为
void calculator(...)
[解决办法]
calculator ()函数没有声明返回类型,你在前面加个“void”.
[解决办法]
Type mismatch in redeclaration of 'calculator
这句话什么意思,你定义的calculator的类型不匹配。哪里不匹配呢?
你找下
calculator(x,y,add); 你用的时候返回的float类型,而你在声明的时候返回是什么类型
float (*pfunc),为什么要加(*pfunc这个东东呢
[解决办法]
#include <stdlib.h>//声明函数 float add (float a, float b); float sub (float a, float b); float mul (float a, float b); float div (float a, float b); void calculator(float a, float b, float (*pfunc)(float, float));//here, declare it firtlyint main() { float x,y;//定义两个实型变量 printf("input x and y:"); scanf("%f,%f", &x, &y);//输入两个数 printf("x+y=");//求两个数的和 calculator(x, y, add); printf("x-y=");//求两个数的差 calculator(x, y, sub); printf("x*y=");//求两个数的积 calculator(x, y, mul); printf("x/y=");//求两个数的商 calculator(x, y, div); return 0;} float add (float a, float b){return a + b;}//定义求和函数 float sub (float a, float b){return (a - b);}//定义求差函数 float mul (float a, float b){return (a * b);}//定义求积函数 float div (float a, float b){return (a / b);}//定义求商函数 //定义调用以上四个函数中某个函数的函数 void calculator(float a, float b, float (*pfunc)(float, float)) { float result; result = (*pfunc)(a, b);//用指向函数的指针调用函数 printf("%f\n", result); }
[解决办法]
main中加上calculator的申明
calculator(float a, float b, float (*)(float, float));
[解决办法]
#include<stdio.h>void calculator(float a, float b, float (*pfunc)(float, float)); //定义下calculatorvoid main() { //声明函数 float add(float a,float b); float sub(float a,float b); float mul(float a,float b); float div(float a,float b); float x,y;//定义两个实型变量 printf("input x and y:"); scanf("%f%f",&x,&y);//输入两个数 //输入函数格式符中间不能有',' printf("x+y=");//求两个数的和 calculator(x,y,add); printf("x-y=");//求两个数的差 calculator(x,y,sub); printf("x*y=");//求两个数的积 calculator(x,y,mul); printf("x/y=");//求两个数的商 calculator(x,y,div); } float add(float a,float b)//定义求和函数 {return (a+b);} float sub(float a,float b)//定义求差函数 {return (a-b);} float mul(float a,float b)//定义求积函数 {return (a*b);} float div(float a,float b)//定义求商函数 {return (a/b);} //定义调用以上四个函数中某个函数的函数 void calculator(float a, float b, float (*pfunc)(float, float)) { float result; result = (*pfunc)(a, b);//用指向函数的指针调用函数 printf("%f\n",result); }
[解决办法]
--------------------------
帖子题目上面的“管理帖子”
[解决办法]
固然 把代码写规范肯定能解决问题
不过我觉得这个编译错误提示的确有些迷惑
没有声明calculator而调用了,
理论上应报错:函数没定义
这说明编译器遇到未定义的函数时根据调用的参数类型自然生成了它的声明
接下来
这个自动生成的声明为什么和下面的实际定义不符呢?
实际定义没写明返回值类型,按一般编译器处理,是作为返回int处理
这说明自动生成的声明返回值是void
....一大圈...