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


int main(void){
  double a=0,r=0,n=0;
  printf("Enter Constant a:");
  scanf("%lf",&a);
  printf("Enter Constant r:");
  scanf("%lf",&r);
  printf("Enter Variable n:");
  scanf("%lf",&n);

  double an;
  an = geom_rec(a,r,n);    // Line 15

  return 0;
}

double geom_rec(double a,double r,double n){    // Line 20
  double ans=a;
  return a;
}

错误:

Line 20: error: conflicting types for 'geom_rec'
Line 15: error: previous implicit declaration of 'geom_rec' was here
4

3 回答 3

12

您忘记为函数创建原型。

将以下内容放在您的main函数之前(您也可以将整个函数移到上面main):

double geom_rec(double a,double r,double n);

如果您调用之前未定义或原型化的函数,编译器会假定它返回int- 这与您的实际返回类型相冲突。

于 2012-04-18T15:13:22.413 回答
4

您可以将函数的原型放在之前main(),也可以将函数本身放在之前main()

于 2012-04-18T15:14:05.770 回答
0

当编译器到达第 15 行时,它之前没有见过函数geom_rec,所以它假设函数返回int

稍后,在第 20 行,您将函数定义为返回 adouble并接受 3 个double参数,这与编译器“知道”该函数不同。所以它抱怨,让你有机会在使用它之前为函数定义一个适当的原型。

于 2012-04-18T15:26:31.297 回答