【问题标题】:error: conflicting types for 'components' [duplicate]错误:“组件”的类型冲突[重复]
【发布时间】:2018-01-08 17:49:32
【问题描述】:

谁能指出我做错了什么,我对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;
}

我将不胜感激。

【问题讨论】:

标签: c compiler-errors


【解决方案1】:

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

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;    
}

【讨论】:

  • 如果我现在想在组件函数 x = vx.t , y = vy*t + ½ at^2 where a= -9.81 中添加它怎么办
  • 对不起?我不明白你在问什么。你能改写一下吗?
  • 如果您想以不同的方式计算两个值,即使用不同的数学表达式,那么是什么阻止您编辑使用的表达式但保留返回结果的方式?
  • 我不完全理解你的意思,你能扩展一下吗?
  • 不,我不能。我猜你想问/告诉我什么。如果我猜错了,那么请您尝试(以不同的方式)解释我可以为您提供什么帮助。但也许你应该对此提出一个单独的新问题。
猜你喜欢
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 2016-02-07
  • 2013-03-25
  • 1970-01-01
  • 2014-03-10
  • 2012-10-04
  • 2013-04-24
相关资源
最近更新 更多