gpt4 book ai didi

iphone - 浮点函数返回值

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:56 25 4
gpt4 key购买 nike

我正在编写我的对象优先应用程序,但我不明白为什么编译器会给出我的错误。(使用 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;
}

最佳答案

尝试在 main 之前声明 res,以便编译器在 main 中找到它时知道它。如果你不事先声明它,会发生什么:

  1. 编译器首先在 main 主体中遇到 res

  2. 它根据在 main< 中调用 res 的方式推断出的内容,为 res 构成了一个“隐式声明”/;根据 C 约定,这意味着 int 返回类型;

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

关于iphone - 浮点函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com