• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Parse errors when declaring local variables.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parse errors when declaring local variables.


  • Subject: Re: Parse errors when declaring local variables.
  • From: Greg Titus <email@hidden>
  • Date: Tue, 11 Feb 2003 08:54:20 -0800

On Tuesday, February 11, 2003, at 01:03 AM, Tim James wrote:
/* FileDragTableView.h */

#import <Cocoa/Cocoa.h>
#import "MainWindowController.h"

@interface FileDragTableView : NSTableView {
IBOutlet MainWindowController *controller;
BOOL tableIsHighlighted;
}
// Bunch of method declarations here.
@end


/* MainWindowController.h */

#import <Cocoa/Cocoa.h>
#import "FileDragTableView.h"

@interface MainWindowController : NSObject {
IBOutlet FileDragTableView *mp3Table;
}
// Method declarations here.
@end

It's because you have two headers that are importing each other. If you tried to compile FileDragTableView.m, for instance, it would import FileDragTableView.h, which would import MainWindowController.h, which would see the import line for "FileDragTableView.h" again, but since the import directive makes sure that imported files aren't duplicated it would be skipped. You are left with the preprocessor producing:

---------------------
/* FileDragTableView.h */
#import <Cocoa/Cocoa.h>
/* MainWindowController.h */
#import <Cocoa/Cocoa.h> // already imported, so does nothing
#import "FileDragTableView.h" // already imported, so does nothing

@interface MainWindowController : NSObject {
IBOutlet FileDragTableView *mp3Table;
}
// Method declarations here.
@end

@interface FileDragTableView : NSTableView {
IBOutlet MainWindowController *controller;
BOOL tableIsHighlighted;
}
// Bunch of method declarations here.
@end
-----------------------

The compiler then sees this, reaches the declaration of mp3Table, and has no idea what a FileDragTableView is, because it hasn't seen the declaration for it yet. Thus your error.

What you need to do is use the @class directive to tell the compiler, basically, "here is a class name that I will declare in full later on". If you change your headers to look like these, they'll compile correctly:

----------------------------------------
/* FileDragTableView.h */

#import <Cocoa/Cocoa.h>

@class MainWindowController;

@interface FileDragTableView : NSTableView {
IBOutlet MainWindowController *controller;
BOOL tableIsHighlighted;
}
// Bunch of method declarations here.
@end


/* MainWindowController.h */

#import <Cocoa/Cocoa.h>

@class FileDragTableView;

@interface MainWindowController : NSObject {
IBOutlet FileDragTableView *mp3Table;
}
// Method declarations here.
@end
-------------------------------------------

A good practice to follow is to never import any class .h into another class .h except for the header for the superclass. Use @class declarations instead. Then #import the header for the class you are using in the .m file instead of the .h file. Not only does this avoid preprocessor loops like the one you are seeing, it also gives the compiler less code to work with so you get faster compiles and smaller precompiled headers.

Hope this helps,
- Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Parse errors when declaring local variables. (From: Tim James <email@hidden>)

  • Prev by Date: WSMakeStubs code issues
  • Next by Date: RE: WSMakeStubs code issues
  • Previous by thread: Parse errors when declaring local variables.
  • Next by thread: Problems with SecKeychainOpen
  • Index(es):
    • Date
    • Thread