Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 577
JHHoang Avatar asked Jan 24 '12 22:01

JHHoang


People also ask

What does implicit declaration of function is invalid in c99 mean?

Code in header files should NOT call functions. 9/30/2020. NKurzman. First you have the Option to tell the compiler to use C90 ode. implicit declaration of function means you do not have Prototypes for your functions.

What is implicit declaration error in C?

Such an 'implicit declaration' is really an oversight or error by the programmer, because the C compiler needs to know about the types of the parameters and return value to correctly allocate them on the stack.


2 Answers

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]; 
like image 178
Richard J. Ross III Avatar answered Sep 23 '22 09:09

Richard J. Ross III


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];
like image 34
AtkinsonCM Avatar answered Sep 23 '22 09:09

AtkinsonCM