0

我正在编写我的对象第一个应用程序,但我不明白为什么编译器会给出我的错误。(使用 int 代码工作......)

#import <Foundation/Foundation.h>

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

    @autoreleasepool {


        // 1st var
        NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));

    }
    return 0;
}
float res (float a, float b, float c)
{
    float res=a+b+c;
    return res;
}
4

3 回答 3

4

尝试res在 main 之前声明,以便编译器在main. 如果你不事先声明它,会发生什么:

  1. 编译器第一次遇到resbody main

  2. 它构成了一个“隐式声明” res,基于它可以从res被称为 inside的方式推断出来的内容mainint根据 C 约定,这意味着返回类型;

  3. res稍后找到真实的时,推断的签名(即返回类型)与真实的不匹配会触发编译错误。

要解决这个问题:

float res (float a, float b, float c);

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

    // 1st var
    NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));
  }
  return 0;
}

float res (float a, float b, float c)
{
  float res=a+b+c;
  return res;
}
于 2012-12-21T11:04:10.617 回答
1

你只是忘记声明函数,只是把

float res (float a, float b, float c);

在你之前int main

希望对你有帮助!

于 2012-12-21T11:06:56.850 回答
0

尝试:

#import <Foundation/Foundation.h>

float res (float a, float b, float c)
{
    float res=a+b+c;
    return res;
}

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

    @autoreleasepool {


        // 1st var
        NSLog(@"Hi, %f World!", res(1.0f, 2.0f, 3.0f));

    }
    return 0;
}
于 2012-12-21T11:06:34.127 回答