Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problem with class inheritance

Hello,
I have a problem making my own class inheritance, i'm new in Objective C, i have been programing for a few weeks in a iPhone game.

I get this error:
"error: cannot find interface declaration for 'WPToucher', superclass of 'WPVersusTouch'"

I made a search about this error and i found that could be a recursive import problem, or something related with imports statments... but i cant really see the problem on my code...

I will explain how i want to make the class inheritance:

Super class WPToucher witch inherits from UIView
and WPVersusTouch witch should inherits from WPToucher
and other WPSurvivalTouch (i didn't created this yet) witch will inherit from WPToucher also

then:
WPToucher : UIView
WPVersusTouch : WPToucher

ok?

Now my code:

WPToucher.h

#import <UIKit/UIKit.h>
#import "GameYard.h"
@class GameYard;
@interface WPToucher : UIView {
GameYard *yard;
}
@property(nonatomic,retain) GameYard *yard;
-(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position;
-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event;
- (id) initWithYard: (GameYard *) yardView;
@end


WPToucher.m

#import "WPToucher.h"
@implementation WPToucher
@synthesize yard;
- (id) initWithYard: (GameYard *) yardView {
self = [super init];
if (self != nil) {
yard=yardView;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position {
}
@end


WPVersusTouch.h

#import <Foundation/Foundation.h>
#import "WPToucher.h"
@class WPToucher;
@interface WPVersusTouch : WPToucher {
}
@end


WPVersusTouch.m

#import "WPVersusTouch.h"
@implementation WPVersusTouch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
......
}
-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event{
.....
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
.....
}
-(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position {
.....
}
@end



Thanks in advance!

Message was edited by: sebasgre

Message was edited by: sebasgre

Mac OS X (10.5.7)

Posted on Jun 10, 2009 1:54 PM

Reply
Question marked as Best reply

Posted on Jun 10, 2009 8:49 PM

I couldn't reproduce the error you described. After pasting your code into a test bed, there were no compiler warnings or errors, and no runtime errors when I made an instance of WPVersusTouch using initWithYard. Of course I had to make a dummy class for GameYard since I didn't have that interface. What's in that class? Does it #import WPToucher.h or WPVersusTouch.h?

The "cannot find interface for ...superclass" error can be caused by not importing WPToucher.h into WPVersusTouch.m. But in the code you posted, WPToucher.h is imported into WPVersusTouch.h, and is thus visible to the .m as well.
3 replies
Question marked as Best reply

Jun 10, 2009 8:49 PM in response to sebasgre

I couldn't reproduce the error you described. After pasting your code into a test bed, there were no compiler warnings or errors, and no runtime errors when I made an instance of WPVersusTouch using initWithYard. Of course I had to make a dummy class for GameYard since I didn't have that interface. What's in that class? Does it #import WPToucher.h or WPVersusTouch.h?

The "cannot find interface for ...superclass" error can be caused by not importing WPToucher.h into WPVersusTouch.m. But in the code you posted, WPToucher.h is imported into WPVersusTouch.h, and is thus visible to the .m as well.

Jun 11, 2009 8:45 AM in response to RayNewbie

Thanks! You were right... not in GameYard but it was in other class that was imported on GamYard...
My doubt now is... i want to import WPToucher on GameYard because I will have a method that will allow any WPToucher class...
Should I use @class statement to tell the compiler that WPToucher exists and it was imported in other file or what??

Thanks again Ray

Jun 11, 2009 9:55 PM in response to sebasgre

Generally, you only need to #import the @interface of classA into the @interface of classB when B is to be a subclass of A. Otherwise you only need a @class statement (sometimes called a forward declaration).

Using @class instead of #import prevents a loop when classB has a member of type classA, and classA has a member of type classB. In that case classB can't be defined without knowing the definition of its classA member. But knowing the structure of classA requires the definition of its classB member. So defining classB requires the definition of classB.

Except for the case of a superclass-subclass relationship, just remember: @class in the @interface, #import in the @implementation, and you should be fine.

Problem with class inheritance

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.