如果此问题可以帮助您,请投票.:)
我一直在寻找解决方案,但没有找到任何有用的解决方案.我得到错误: - C99中函数'sum'的隐式声明无效 - C99中函数'average'的隐式声明无效 - '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;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当编译器看到您使用sum它的代码时,它不知道具有该名称的任何符号.您可以转发声明它来解决问题.
int sum (int values[], int count);
Run Code Online (Sandbox Code Playgroud)
放在那之前main().这样,当编译器看到第一次使用sum它时就知道它存在并且必须在其他地方实现.如果不是那么它将产生线性错误.
| 归档时间: |
|
| 查看次数: |
15060 次 |
| 最近记录: |