5

私はこれに対する解決策を探していましたが、役立つものは何も見つかりませんでした。次のエラーが発生します。

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;
    }
4

2 に答える 2

9

これら2つの関数の宣言を追加するか、2つの関数定義をmainの前に移動する必要があります。

于 2012-12-13T23:20:12.233 に答える
7

問題は、コンパイラが使用するコードを見るまでに、sumその名前のシンボルを認識していないことです。問題を修正するために前方宣言することができます。

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

その前に置いてくださいmain()。このように、コンパイラが最初の使用を確認すると、コンパイラがsum存在し、別の場所に実装する必要があることがわかります。そうでない場合は、ライナーエラーが発生します。

于 2012-12-13T23:18:48.147 に答える