c/c++语言开发共享x的冲突类型和先前的声明在这里……什么?

当我有时间的时候,我一直在教自己C几个月,我遇到了一个问题,我不知道如何修复。

具体来说,当我尝试使用gcc编译它时,我得到:

  geometry.c:8:错误:'trapezoid'的冲突类型  geometry.c:7:注意:之前的'trapezoid'声明就在这里  geometry.c:48:错误:'trapezoid'的冲突类型  geometry.c:7:注意:之前的'trapezoid'声明就在这里  geometry.c:119:错误:'trapezoid_area'的冲突类型  geometry.c:59:注意:'trapezoid_area'的先前隐式声明就在这里  geometry.c:在函数'cone_volume'中:  geometry.c:128:error:被叫对象'3.14100000000000001421085471520200371742248535156e + 0'不是函数  geometry.c:在函数'cylinder_volume'中:  geometry.c:136:错误:被叫对象'3.14100000000000001421085471520200371742248535156e + 0'不是函数 

现在,我想我可能需要对这些function进行类型转换,但我不确定。

看起来它想要读取PI,我将其定义为3.141,作为一个函数。 有没有办法可以避免使用魔法数字3.141(虽然它比其他数字少得多)?

//Geometric formulae #include  #include  #define PI 3.141 float trapezoid(float b1, float b2, float h); int trapezoid(); float sphere_volume(float r); int sphere(); float cone_volume(float r, float h); int cone(); float pyramid_volume(float b, float h); int pyramid(); float cylinder_volume(float r, float h); int cylinder(); int main(void) { char selection = 0; while(selection != 'q') { printf("t--Geometric Formulas--n"); printf("tSelection an option from the menu: "); scanf("%c", & selection); switch(selection) { case 1: //Trapezoid area printf("%d", trapezoid()); break; case 2: //Sphere volume printf("%d", sphere()); break; case 3: //Cone volume printf("%d", cone()); break; case 4: //Pyramid volume printf("%d", pyramid()); break; case 5: //Cylinder volume printf("%d", cylinder()); break; default: break; } } } // --Shape Menus-- //Trapezoid int trapezoid() { float h = 0, b1 = 0, b2 = 0; float traparea; printf("tTrapezoid base 1: "); scanf("%3f", &b1); printf("tTrapezoid base 2: "); scanf("%3f", &b2); printf("tTrapezoid height: "); scanf("%3f", &b2); traparea = trapezoid_area(b1, b2, h); printf("tTrapezoid Area: %3fn", traparea); } //Sphere int sphere() { float r = 0; float spherevol; printf("tSphere radius: "); scanf("%f", &r); spherevol = sphere_volume(r); printf("tSphere volume: %3fn", spherevol); } //Cone int cone() { float r = 0, h = 0; float conevol; printf("tCone radius: "); scanf("%f", &r); printf("tCone height: "); scanf("%f", &h); conevol = cone_volume(r, h); printf("tCone volume: %3fn", conevol); } //Pyramid int pyramid() { float b = 0, h = 0; float pyramidvol; printf("tPyramid base: "); scanf("%f", &b); printf("tPyramid height: "); scanf("%f", &h); pyramidvol = pyramid_volume(b, h); printf("tPyramid volume: %3fn", pyramidvol); } //Cylinder int cylinder() { float r = 0, h = 0; float cylindervol; printf("tCylinder radius: "); scanf("%f", &r); printf("tCylinder height: "); scanf("%f", &h); cylindervol = cylinder_volume(r, h); printf("Cylinder volume: %3f", cylindervol); } // --Geometric Formulas-- //Trapezoid Area Computation float trapezoid_area(float b1, float b2, float h) { return ((b1 + b2) * h) / 2; } //Sphere Volume Computation float sphere_volume(float r) { return ((pow(r,3)) * (4 * PI)) / 3; } //Cone Volume Computatioin float cone_volume(float r, float h) { return ((PI(pow(r,2)) * h)) / 3; } //Pyramid Volume Computation float pyramid_volume(float b, float h) { return (b * h) / 3; } //Cylinder Volume Computation float cylinder_volume(float r, float h) { return (PI(pow(r,2))) * h; } 

    他们中的任何一个都说有一个“隐含的”定义:这可以通过预先声明来解决。 例如,您预先声明了float trapezoidint trapezoid ,但是您没有预先声明trapezoid_area 。 正如其他人指出的那样,你也不能像在C ++中那样在C中重载。

    在一些领域,你试图进行隐式乘法 – 例如, PI(pow(r,2))应该是PI *(pow(r,2))

    您看到的错误是因为您使用不同的签名定义了两次相同的function

     float trapezoid(float b1, float b2, float h); int trapezoid(); 

    根据您的其他定义,看起来第一个trapezoid函数应该命名为trapezoid_volume

     float trapezoid_volume(float b1, float b2, float h); int trapezoid(); 

    C不支持函数重载。 C ++可以。

    由于您不小心将trapezoid_volume声明为trapezoid ,因此会出现编译错误。

    你定义了两次函数梯形。 C不是C ++。 您不能像使用重载function在C ++中那样使用不同的签名定义相同的函数。

     #define PI 3.141 float trapezoid(float b1, float b2, float h); int trapezoid(); 

    我想你想要的

     float trapezoid_volume(...); /* ^^^^^^^ */ 

    要回答关于定义PI的问题 – pi通常在math.h定义(作为M_PI ),但由于它不是实际标准的一部分,因此您可能需要进行一些配置才能获得它。

    例如,在使用MSVC时,您需要定义_USE_MATH_DEFINES来获取它。 看到

    如果您的平台没有它,您可能希望将以下内容添加到标题中:

    需要了解更多c/c++开发分享x的冲突类型和先前的声明在这里……什么?,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

     #ifndef #define M_PI 3.14159265358979323846264338327 #endif 

      以上就是c/c++开发分享x的冲突类型和先前的声明在这里……什么?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/979076.html

      (0)
      上一篇 2021年12月12日
      下一篇 2021年12月12日

      精彩推荐