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

发布于 2024-12-28 16:22:49 字数 735 浏览 1 评论 0原文

当我尝试比较

.h 文件中两个 UIColors 的 RGB 分量时,我收到此警告,我

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

在 .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));
  }

声明了此内容然后在同一个 .m 文件中,我像这样比较 2 个 UIColors

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

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

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

In .h file, I declared this

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

In .m file

 - (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

 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的笑 2025-01-04 16:22:49

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

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

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

或者,您可以将调用更改为:

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

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];
凉城凉梦凉人心 2025-01-04 16:22:49

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

如果 .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));
}

那么您应该在 .h 中声明它:

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

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

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

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

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

 - (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:

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

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

int d = [self ColorDiffBetweenColorOne:C1 AndColorTwo:C2];
旧竹 2025-01-04 16:22:49

h. 文件中缺少原型!

There is a prototype missing in the h.file!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文