【问题标题】:Conflicting types and previous declaration of x was here...what?冲突的类型和先前的 x 声明在这里......什么?
【发布时间】:2011-07-28 00:04:16
【问题描述】:

我已经有几个月的时间自学了 C 语言,但我遇到了一个不知道如何解决的问题。

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

geometry.c:8: 错误:“梯形”的类型冲突 geometry.c:7: 注意:之前的“梯形”声明在这里 geometry.c:48: 错误:“梯形”的类型冲突 geometry.c:7: 注意:之前的“梯形”声明在这里 geometry.c:119:错误:“trapezoid_area”的类型冲突 geometry.c:59: 注意:之前隐含的“trapezoid_area”声明在这里 geometry.c:在函数“cone_volume”中: geometry.c:128: 错误: 调用对象‘3.14100000000000001421085471520200371742248535156e+0’不是函数 geometry.c:在函数“cylinder_volume”中: geometry.c:136: 错误: 调用对象‘3.14100000000000001421085471520200371742248535156e+0’不是函数

现在,我想我可能需要对函数进行类型转换,但同样,我不确定。

它看起来想将我定义为 3.141 的 PI 作为函数读取。有没有办法避免使用幻数 3.141(尽管它比其他幻数少得多)?

//Geometric formulae
#include <stdio.h>
#include <math.h>

#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: %3f\n", traparea); }

//Sphere
int sphere() {
        float r = 0;
        float spherevol;

        printf("\tSphere radius: ");
        scanf("%f", &r);

        spherevol = sphere_volume(r);

        printf("\tSphere volume: %3f\n", 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: %3f\n", 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: %3f\n", 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; }

【问题讨论】:

    标签: c function void


    【解决方案1】:

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

    在您尝试进行隐式乘法的一些领域 - 例如,PI(pow(r,2)) 应该是 PI * (pow(r,2)) .

    【讨论】:

    • 我不介意提名,但是……我只回答了问题的一小部分。您确定要选择我的作为正确答案吗?
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 不同的签名会产生这种类型的错误(已确认)。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 谢谢...已修复...但现在显示 /tmp/ccn6YvQ0.o: In function sphere_volume': geometry.c:(.text+0x39d): undefined reference to pow' /tmp/ccn6YvQ0.o: In function cone_volume': geometry.c:(.text+0x3d0): undefined reference to pow' / tmp/ccn6YvQ0.o: 在函数cylinder_volume': geometry.c:(.text+0x41b): undefined reference to pow' collect2: ld 返回 1 退出状态
      • @Bowlslaw:你还没有链接数学库。如果您使用的是 linux,请尝试使用带有 -lm 选项的 gcc。
      • -lm 选项有效,即使我已经链接了 math.h 库。
      【解决方案5】:
      #define PI 3.141
      
      float trapezoid(float b1, float b2, float h);
      int trapezoid();
      

      我想你想要

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

      【讨论】:

        【解决方案6】:

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

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

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

        #ifndef
        #define M_PI 3.14159265358979323846264338327
        #endif
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-09
          • 1970-01-01
          相关资源
          最近更新 更多