【问题标题】:Error: Conflicting types for function (C) [duplicate]错误:函数(C)的类型冲突[重复]
【发布时间】:2016-01-03 23:55:40
【问题描述】:

所以我正在制作一个制作表格的函数,它将 x 和 y 值放在一个方程中。这是我目前所拥有的。

#include <stdio.h>
#include <math.h>
int main(){
    int x, y;
    float num;

    printf("%3c", '+');
    for (x=5; x <= 100;x=x+5){
        printf("%8i",x);
    }
    printf("\n");
    for (y = 5; y<= 100;y=y+5){
        printf("%3d ",y);
        for (x=5;x<=100;x=x+5){
            num = theMath(x, y);    
            printf("%7f", num);
            printf(" ");    
        }
        printf("\n");

    }
    return 0;
}

float theMath(int x, int y){
    float sum;
    sum = ((x*x*x*x)/(y*y)) + sqrt(y);
    return sum;
}

它告诉我“错误:'theMath' 的类型冲突”,我不知道为什么。用 gcc 编译。

【问题讨论】:

  • 你是怎么编译这个的?
  • 我正在用 gcc 编译
  • 你能粘贴 gcc 行吗(即gcc test.c -lm -o test.o)?
  • gcc xtable.c -o xtable -lm

标签: c function types floating-point


【解决方案1】:

在函数调用之前添加 theMath 的原型定义(函数声明)。这应该是错误的原因。

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

//add declaration of the function here
float theMath(int x, int y);
int main(){
  int x, y;
  float num;

  printf("%3c", '+');
  for (x=5; x <= 100;x=x+5){
    printf("%8i",x);
  }
  printf("\n");
  for (y = 5; y<= 100;y=y+5){
    printf("%3d ",y);
    for (x=5;x<=100;x=x+5){
      num = theMath(x, y);  
      printf("%7f", num);
      printf(" ");  
    }
    printf("\n");

  }
  return 0;
}

float theMath(int x, int y){
  float sum;
  sum = ((x*x*x*x)/(y*y)) + sqrt(y);
  return sum;
}

【讨论】:

  • 不是我的反对意见。然而,有一个定义。他实际上应该添加一个prototype declaration。请不要混淆这两个术语!或者在使用函数之前移动定义。
  • 不,这不是“原型定义”(这将是具有原型样式签名的 定义,如果您不使用额外声明,则为标准,而只是在使用之前定义(意味着声明)函数。
  • 不管怎样,这是一个骗局。
【解决方案2】:

您的函数 theMath 在定义之前使用,并且在使用之前未声明。您只需在 C 文件的开头声明该函数:

   float theMath(int x, int y);  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多