0

I'm very confused about an error I'm getting. I have the following code:

In View1.m I make this call:

[iconView addIconWithType:IconTypeStandard];

IconView.h

typedef enum {
    IconTypeStandard = 0,
    IconTypeNew = 1,
} IconType;

-(void)addIconWithType:(IconType)iconType;

IconView.m

-(void)addIconWithType:(IconType)iconType {

...

}

Icon.h

#import "IconView.h"


@interface Icon : UIView {

}

-(Icon *)initWithFrame:(CGRect)frame;

-(void)type:(IconType)iconType;

That line is causing an error of 'Expected ')' before 'IconType'', but i have no idea why?

Icon.m

-(void)type:(IconType)iconType {

...

}
jscs
  • 63,694
  • 13
  • 151
  • 195
Andrew
  • 15,935
  • 28
  • 121
  • 203
  • What's the code preceding the line that is causing the error? – Deepak Danduprolu May 17 '11 at 20:21
  • 1
    Maybe the problem is above those lines, like not terminating the method before, or not within the @implementation block? – Eiko May 17 '11 at 20:32
  • I put a -(void) test; above that line and commented out the problematic line, then the problem disappears. So it must be a problem with that line. – Andrew May 17 '11 at 20:34
  • May be that 'type' in the beginning of the name of the function is processed like a keyword or other function name? – danny_23 May 17 '11 at 20:35
  • Nope, changing it to simply be called 'test' still shows the same error. – Andrew May 17 '11 at 20:39
  • If doing a clean and rebuild doesn't fix it, you might have to start erasing and retyping lines. That's an old-school solution, but it has worked many times. – Rayfleck May 17 '11 at 20:42
  • I don't get the file structure. What is in IconView.h? Is the typedef within the @interface declaration? Not sure how Icon and IconView correspond (especially as Icon is a UIView). Things can get tricky if the IconView imports something from Icon which itself imports IconView. import is much better than include, but not perfect. – Eiko May 17 '11 at 20:57
  • i did a clean and rebuild. replacing that (IconType) with (int) works, but it isn't a way around i should take, because i've then got to change it in 2 places every time that bit gets modified. – Andrew May 17 '11 at 21:00
  • If i move the typedef into Icon.h, i get the same error i was getting, but this time in IconView.h. Somehow, despite both having their .h files imported, it's not carrying over properly? – Andrew May 17 '11 at 21:01
  • 2
    Check this [question](http://stackoverflow.com/questions/1977205/using-enums-as-parameters-in-an-external-file-in-objective-c). Something wrong with a declaration i think. – danny_23 May 17 '11 at 21:06
  • That's it. Thanks Danny. On iconview.h i had imported icon.h, which was causing the problem. – Andrew May 17 '11 at 21:15

2 Answers2

2

You have a trailing comma in your enum

IconTypeNew = 1,    // remove the comma
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • I changed that, it hasn't fixed it. – Andrew May 17 '11 at 20:24
  • C99 allows a [trailing comma](http://stackoverflow.com/questions/792753/is-the-last-comma-in-c-enum-required) between the last enumerator and the closing brace. – albertamg May 17 '11 at 20:28
  • 1
    Not sure why this gets so many upvotes, as the comma is perfectly legal (see http://stackoverflow.com/questions/792753/is-the-last-comma-in-c-enum-required). No downvote from me though. :) – Eiko May 17 '11 at 20:30
2

I am guessing there is a cyclical inclusion. Icon.h refers to IconView.h and vice versa. Since the typedef comes after the import statement in Icon.h it is raising the error as it can't find IconType yet.

You should probably declare @class IconView; and move the #import "IconView.h" to Icon.m.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Icon.h has to import IconView.h or it won't be able to see the typedef for `IconType`. An `@class Icon` declaration could go in IconView.h, however. – jscs May 17 '11 at 21:50
  • I read somewhere in the comments that he had moved the `IconType` declaration to `Icon.h` so I suggested as I did. But thats all done now. It seems to have solved the problem. – Deepak Danduprolu May 18 '11 at 04:33