Implementing class: Cannot find superclass
I have ForceGaugeViewController class and ForceGaugeController class. I'm trying to make the ForceGaugeController class be a subclass of ForceGaugeViewController, but I'm receiving errors.
Error:
Cannot find interface declaration for ForceGaugeViewController superclass of ForceGaugeController class.
ForceGaugeViewController.h
#import <UIKit/UIKit.h>
#import <math.h>
#import "HardwareController.h"
#import "ForceGaugeController.h"
@interface ForceGaugeViewController : UIViewController{
}
end
ForceGaugeViewController.m
#import "ForceGaugeViewController.h"
@implementation ForceGaugeViewController
ForceGaugeController开发者_JS百科.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
@class ForceGaugeViewController;
// error here
@interface ForceGaugeController : ForceGaugeViewController{
}
@end
ForceGaugeController.m
#import "ForceGaugeController.h"
You can not only forward reference a class that you will inherit from or a protocol that you will implement. You just need to import the superclass in the header which you are already doing and then remove the @class
declaration.
Edit: Also the superclass ForceGaugeViewController
should not include the subclass ForceGaugeViewController
in the header file.
ForceGaugeController.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
@interface ForceGaugeController : ForceGaugeViewController{
}
@end
Be sure to avoid circular imports: Make sure you do not import any of the header or implementation subclasses files into the superclass file where the superclass interface declaration is (.h file)
You're including ForceGaugeController.h in ForceGaugeViewController.h and including ForceGaugeViewController.h in ForceGaugeController.h. Circular includes will confuse the compiler and that's probably what's wrong.
A header file for a class only needs to include the framework (ie. UIKit), the subclass, and any protocols the class conforms to. Forward declarations will do for the classes of instance instance variables/method arguments/properties.
Remove the line:
@class ForceGaugeViewController;
You are already importing the ForceGaugeViewController class via its header file at the top of your ForceGaugeController.m file.
EDIT 1
And as pointed out by @Chris Devereux, you have a circular reference in your header files, you will want to get rid of that as well.
EDIT 2
And not sure how I missed the self-import in your ForceGaugeController.h file. I assume that it's a typo, but if not I'm sure you know you have to remove it.
精彩评论