gpt4 book ai didi

c - 错误: conflicting types for 'components'

转载 作者:行者123 更新时间:2023-11-30 16:37:00 25 4
gpt4 key购买 nike

有人可以指出我做错了什么吗,我对 C 编程很陌生。我不断收到“组件”错误的类型冲突。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
float Vo,ang;
printf("Enter Velocity:");
scanf("%f",&Vo);
printf("Enter Angle:");
scanf("%f",&ang);
printf("The x component is %f and the y component is %f",components(Vo,ang));

}

float components(float Vo, float ang,float Vx, float Vy)
{
Vx=Vo*cos(ang);
Vy=Vo*sin(ang);
return Vx,Vy;
}

如果有任何帮助,我将不胜感激。

最佳答案

将被调用函数的定义移到调用它的位置之前。或者在main()之前提供原型(prototype),即插入

float components(float Vo, float ang,float Vx, float Vy);

之前

int main()

这样编译器就不必猜测调用该函数所涉及的类型。由于编译器猜测不正确,它稍后会提示类型不匹配。更改编译器在修复该问题时看到的内容的顺序。

另外,

components(Vo,ang)

在提供给 printf 的值中,仅给出一个值,而带有格式字符串的 printf() 需要两个值。
您需要将两个单独的值写入两个变量,然后将其提供给 printf。例如,您可以为函数提供另外两个参数,即指向合适变量的指针。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void components(float Vo, float ang, float* outpVx, float* outpVy)
{
*outpVx=Vo*cos(ang);
*outpVy=Vo*sin(ang);
}

int main(void)
{
float Vo,ang;
float outVx=0.0;
float outvy=0.0;

printf("Enter Velocity:");
scanf("%f",&Vo);
printf("Enter Angle:");
scanf("%f",&ang);
components(Vo,ang, &outVx, &outVy);
printf("The x component is %f and the y component is %f", outVx, outVy);

return 0;
}

关于c - 错误: conflicting types for 'components' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com