7

I've got some code where my classes inherit from a superclass, and everything has been working fine till now. I'm getting an error whenever I try to use any of the superclass variables, saying that they are undeclared (first use in this function). It's only happening in one of my subclasses, & it looks exactly the same as the others. I'm wondering if there's anything obvious which I should know about (being quite new to Objective-C). The basic code is like -

@interface mySuperClass : UIViewController {

BOOL myVar;

}

Then -

@interface mySubClass : mySuperClass {

}

@implementation mySubClass {

-(void)someMethod {

    myVar = YES; // error here

}

@end

Any help much appreciated - if you need more info, let me know! Thanks.

Kay
  • 12,918
  • 4
  • 55
  • 77
SomaMan
  • 4,127
  • 1
  • 34
  • 45
  • Why not write the exact error you are having? Also that should work aside from the typos and missing `@end`s. Post a minimal complete example that reproduces the problem. – Georg Fritzsche May 12 '11 at 11:39
  • It would be difficult to post anything meaningful, as there's a lot of code, and the program runs fine if I comment out the variables (apart from the effect the vars have on other things...). I guess it might be something deeper in the code, but there's such a lot of it I would feel bad posting it all! I'm going to rebuild the entire implementation and see if I get the same thing. As I say, other subclasses done the same way work fine, I'm trying hard not to put it down to an XCode 4 bug... – SomaMan May 12 '11 at 13:03
  • Hm, maybe you end up with a weird include that doesn't have the instance var `myVar` in the interface? You could try looking at the preprocessed source file that has the error to see how the interface definition for `mySuperClass` looks in there? – Georg Fritzsche May 12 '11 at 13:26
  • Thanks very much for all your help, but unfortunately none of the suggestions here are finding the cause of the problem! I've re-written the code (copy & paste the whole lot into a new .m, bit by bit to see if I get an error) and there's no error at all this time. It has to be an XCode bug, just a shame I wasted a day & all your time finding out. I'm off for an ice cream... – SomaMan May 13 '11 at 09:45

4 Answers4

4

I just got over a very similar strange error where I could no longer access properties in my superclass, and xcode was givine me compiler errors saying "(*) undeclared (first use in this function)". However I had not any problems in the past...

The problem was that I had introduced typos at the top of my .m file and the xcode compiler output was misleading me. Specifically I had @synthesize statements where the properties were misspelled, either in the synthesize statement or in the corresponding variable in the headerfile.

If you have @synthesize statements or other declarations, examine them with a fine toothed comb (i.e. which lines have you introduce most recently?), or even comment out a block of them to see if you can compile again and narrow down the culprit.

Again the compiler errors were very misleading, so it really was tough to debug. Although like 99.9% of the time the error was my own. :)

kris
  • 2,516
  • 25
  • 29
2

I can't see anything wrong with the code you've pasted. My first guess would be that you're not importing the mySuperClass .h file properly. Ie you're missing

#import "mySuperClass.h"    //or #include

In mySubClass.h, or mySubClass.m

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
1

I ran into the same problem and after wasting hours I finally solved it in my project:

I refactored my source and removed a member but forgot to remove the @property definition in .h file. This leads to errors at every place when I am using super members. BTW: Methods from super are fine.

So check your property defs. Thanks to Screwtape in post 8 for his solution :-)

EDIT: Oops, Kris' answer is pretty similar except from typos vs. removal.

Kay
  • 12,918
  • 4
  • 55
  • 77
1

I am having the same issue for weeks

"Variable Undeclared" error when compiling to iOS Device, but not for Simulator

One solution I found is to simply change the compiler from the default LLVM GCC 4.2 to LLVM Compiler 2.0 (or to `Apple LLVM Compiler 2.1). It seems to be a bug in the compiler, but it is just a guess.

Changing it is a quick fix for your problem if there is no need for you to use the GCC compiler at all.

Community
  • 1
  • 1
Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112