#include <stdio.h>
double f1(double x)
{
return x*x;
}
double f2(double x,double y)
{
return x*y;
}
double fun(double a,double b)
{
double (*f)();
double r1,r2;[code=c]
f = f1;
r1=f(a);
f = f2;
r2=(*f)(a,b);
return r1+r2;
}
main ()
{double x1=5,x2=3,r;
r=fun(x1,x2);
printf(“\nx1=%f,x2=%f,x1*x1+x1*x2=%f\n”,x1,x2,r);
}[/code]
哪地方出错了
解决方案:20分
f = f1;
f 的类型是 double (*)()
f1 的函数指针类型是 double (*)(double)
两者不同,无法转换。
f 的类型是 double (*)()
f1 的函数指针类型是 double (*)(double)
两者不同,无法转换。
解决方案:20分
C语言下允许这种隐式转换,但C++不行