ios 警告:“函数 '...' 的隐式声明在 C99 中无效”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8994955/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:28:34  来源:igfitidea点击:

WARNING: "implicit declaration of function '...' is invalid in C99"

iosgcc-warning

提问by JHHoang

I'm getting this warning when I'm trying to compare RGB components of two UIColors

当我尝试比较两个 UIColors 的 RGB 分量时收到此警告

In .h file, I declared this

在 .h 文件中,我声明了这一点

 -(int) ColorDiff:(UIColor *) color1 :(UIColor *)color2;

In .m file

在 .m 文件中

 - (int) ColorDiff:(UIColor *) color1 :(UIColor *)color2{
   ... //get RGB components from color1& color2
   // compute differences of red, green, and blue values
   CGFloat red   = red1   - red2;
   CGFloat green = green1 - green2;
   CGFloat blue  = blue1  - blue2;

  // return sum of squared differences
  return (abs(red) + abs(green) + abs(blue));
  }

And then in same .m file, I compare 2 UIColors like this

然后在同一个 .m 文件中,我比较了 2 个这样的 UIColors

 int d= ColorDiff(C1,C2);// I got the warning right here.

I did research and people say I must include the header file. I did this but didn't help in my case. Why am I getting this error?

我做了研究,人们说我必须包含头文件。我这样做了,但对我的情况没有帮助。为什么我收到这个错误?

采纳答案by Richard J. Ross III

It's because you defined your function as a instance method, not a function. There are two solutions.

这是因为您将函数定义为实例方法,而不是函数。有两种解决方案。

One of which is this to change your method declaration to this:

其中之一是将您的方法声明更改为:

int ColorDiff(UIColor *color1, UIColor *color2) {
    // colorDiff's implementation
}

Or, you can change your call to this:

或者,您可以将呼叫更改为:

int d = [self ColorDiff:C1:C2];

回答by AtkinsonCM

The declaration in your .h file doesn't match your implementation in your .m file.

.h 文件中的声明与 .m 文件中的实现不匹配。

if the implementation of your method in your .m looks like this:

如果您的方法在 .m 中的实现如下所示:

 - (int) ColorDiffBetweenColorOne:(UIColor *) color1 AndColorTwo:(UIColor *)color2
{
    ... //get RGB components from color1& color2
    // compute differences of red, green, and blue values
    CGFloat red   = red1   - red2;
    CGFloat green = green1 - green2;
    CGFloat blue  = blue1  - blue2;

    // return sum of squared differences
    return (abs(red) + abs(green) + abs(blue));
}

than you should declare it like this in .h:

比你应该在 .h 中这样声明它:

- (int) ColorDiffBetweenColorOne:(UIColor *) color1 AndColorTwo:(UIColor *)color2; 

and to call it from that same .m file, use:

并从同一个 .m 文件调用它,使用:

int d = [self ColorDiffBetweenColorOne:C1 AndColorTwo:C2];

回答by Patricia Heimfarth

There is a prototype missing in the h.file!

h.file 中缺少原型!