c - 函数 'sum' 的隐式声明在 C99 中无效

标签 c xcode

我一直在寻找解决方案,但没有找到任何有帮助的东西。 我收到以下错误:

Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function 'average' is invalid in C99
Conflicting types for 'average'

有人以前经历过这种情况吗?我正在尝试在 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;
    }

最佳答案

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

关于c - 函数 'sum' 的隐式声明在 C99 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399069/

相关文章:

c++ - 数组奇怪的参数调理

iphone - 减去两个 NSDate 对象

html - 导航栏大标题-动画问题

xcode - macOS - 有人知道在为 macOS 编程时如何在 Xcode 中缩放 View 吗?

c - 如何从 C 函数中的三个输入创建多项式函数?

c - 当我按下一个键时,如何使这个球体围绕另一个球体旋转?

iphone - 从 C 函数返回 NSString

swift - iOS 13 奇怪的搜索 Controller 差距

c - 在 Windows 中冒泡排序 'works',但在 GNU/Linux 上不使用 GCC

c - 在 printf 中设置可变文本列宽度