2012-12-13 35 views
3

如果此问题对您有帮助,请投票。 :)函数'sum'的隐式声明在C99中无效

我一直在寻找一个解决方案,但没有找到任何帮助。 我收到错误: - 隐式声明的函数'sum'在C99中无效 - 函数'average'的隐式声明在C99中无效 - '平均'的冲突类型 有没有人遇到过?我试图在Xcode中编译它。

#import <Foundation/Foundation.h> 


    int main(int argc, const char * argv[]) 
    { 

     @autoreleasepool 
     { 
      int wholeNumbers[5] = {2,3,5,7,9}; 
      int theSum = sum (wholeNumbers, 5); 
      printf ("The sum is: %i ", theSum); 
      float fractionalNumbers[3] = {16.9, 7.86, 3.4}; 
      float theAverage = average (fractionalNumbers, 3); 
      printf ("and the average is: %f \n", theAverage); 

     } 
     return 0; 
    } 

    int sum (int values[], int count) 
    { 
     int i; 
     int total = 0; 
     for (i = 0; i < count; i++) { 
      // add each value in the array to the total. 
      total = total + values[i]; 
     } 
     return total; 
    } 

    float average (float values[], int count) 
    { 
     int i; 
     float total = 0.0; 
     for (i = 0; i < count; i++) { 
      // add each value in the array to the total. 
      total = total + values[i]; 
     } 
     // calculate the average. 
     float average = (total/count); 
     return average; 
    } 
+0

检查这个答案,为我工作就像一个魅力! https://stackoverflow.com/a/46221365/3063226 – Heitor

回答

7

您需要为这两个函数添加声明,或者在main之前移动两个函数定义。

+0

不是。查看https://stackoverflow.com/a/46221365/3063226,解释和解决方案就在那里! – Heitor

7

的问题是,由当时的编译器看到在您使用sum它不`吨知道有该名称的任何符号的代码。您可以转发声明来解决问题。

int sum (int values[], int count); 

把那个放在main()之前。这样,当编译器看到sum的第一次使用时,它知道它存在并且必须在其他地方实现。如果不是,那么它会给出一个班轮错误。

+0

我解决了它,谢谢你指出我在正确的方向。我还必须包括:int sum(int values [],int count); float average(float values [],int count);上面main() –

+0

完全相同的错误? – imreal

+1

我修好了,谢谢。我也必须补充:float average(float values [],int count); –

相关问题