C 我一直收到错误消息:“我的;'的冲突类型;sqrt&x27&引用;
我刚开始上计算机编程入门课,我只知道2周的编程。 我一直得到“sqrt的冲突类型”,所以我做了一个原型,我仍然得到了信息。我什么都试过了C 我一直收到错误消息:“我的;'的冲突类型;sqrt&x27&引用;,c,function,math,protocols,sqrt,C,Function,Math,Protocols,Sqrt,我刚开始上计算机编程入门课,我只知道2周的编程。 我一直得到“sqrt的冲突类型”,所以我做了一个原型,我仍然得到了信息。我什么都试过了 #include<stdio.h> #include<math.h> float distance(float a, float b, float c, float d); int main() { int a,b,c,d,D; printf("Please enter the first x coordinate.
#include<stdio.h>
#include<math.h>
float distance(float a, float b, float c, float d);
int main()
{
int a,b,c,d,D;
printf("Please enter the first x coordinate. x1= ");
scanf("%f",&a);
printf("Please enter the first x coordinate. y1= ");
scanf("%f",&b);
printf("Please enter the first x coordinate. x2= ");
scanf("%f",&c);
printf("Please enter the first x coordinate. y2= ");
scanf("%f",&d);
D = distance(a,b,c,d);
printf("Distance = %.4f",D);
return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
float d, D, x, y, X, Y;
x = x1 - x2;
y = y1 - y2;
X = x*x;
Y = y*y;
d = X + Y;
float sqrt (float d);
}
#包括
#包括
浮动距离(浮动a、浮动b、浮动c、浮动d);
int main()
{
INTA、b、c、d、d;
printf(“请输入第一个x坐标。x1=”);
scanf(“%f”、&a);
printf(“请输入第一个x坐标。y1=”);
scanf(“%f”和“b”);
printf(“请输入第一个x坐标。x2=”);
scanf(“%f”、&c);
printf(“请输入第一个x坐标。y2=”);
scanf(“%f”、&d);
D=距离(a、b、c、D);
printf(“距离=%.4f”,D);
返回0;
}
浮动距离(浮动x1、浮动x2、浮动y1、浮动y2)
{
浮点数d,d,x,y,x,y;
x=x1-x2;
y=y1-y2;
X=X*X;
Y=Y*Y;
d=X+Y;
浮动sqrt(浮动d);
}
这是一个函数声明
float sqrt (float d);
如果要返回所需函数调用的结果
return sqrt(d);
此外,冲突类型错误是由于sqrt
函数原型
double sqrt(double x);
有一个浮点值
等效值
float sqrtf(float x);
所以也许你的函数应该返回
return sqrtf(d);
注意:我看不出把这个计算结果分成这么多有什么好处
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
我想你是在重新定义sqrt()。删除sqrt()简化前面的float语句:
返回hypotf(x1-x2,y1-y2)代码>