xcode 函数“sum”的隐式声明在 C99 中无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13870227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implicit declaration of function 'sum' is invalid in C99
提问by uplearnedu.com
I've been looking for a solution to this but haven't found anything that will help. I'm getting the following errors:
我一直在寻找解决方案,但没有找到任何有用的东西。我收到以下错误:
Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function 'average' is invalid in C99
Conflicting types for 'average'
Has anyone experienced this before? I'm trying to compile it in Xcode.
有谁之前经历过这个吗?我正在尝试在 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;
}
回答by Lei Mou
You need to add declaration for these two functions, or move the two function definitions before main.
您需要为这两个函数添加声明,或者将两个函数定义移动到 main 之前。
回答by imreal
The problem is that by the time the compiler sees the code where you use sum
it doesn`t know of any symbol with that name. You can forward declare it to fix the problem.
问题是,当编译器看到您使用sum
它的代码时,它并不知道任何具有该名称的符号。您可以转发声明它以解决问题。
int sum (int values[], int count);
Put that before main()
. This way, when the compiler sees the first use of sum
it knows that it exists and must be implemented somewhere else. If it isn't then it will give a liner error.
把那个放在前面main()
。这样,当编译器第一次看到sum
它的使用时就知道它存在并且必须在其他地方实现。如果不是,则会出现班轮错误。