开发者

accessing the parent of an object

I'm trying to call methods on the parent of my object by passing the parent in as property. But i keep getting this error:

expected specifier-qualifier-list before 'Wh开发者_开发技巧eel'

@interface Car : NSObject {
    Wheel *w;
}

- (void)doCarStuff;

@end

@implementation Car
- (id)init {
    if((self = [super init])) {
        //w = [[Wheel alloc] init];
        //w.parent = self;
    }
    return self;
}

- (void)doCarStuff {
    NSLog(@"Car stuff");
}
@end

@interface Wheel : NSObject {
    Car *parent;
}

@property (nonatomic, assign) Car *parent;

@end

@implementation Wheel
@synthesize parent;

- (id)init {
    if((self = [super init])) {
        [parent doCarStuff];
    }
    return self;
}

@end

It is probably because i have to declare the Car before the Wheel and vise versa. I bet the solution is so simple i can't see it :P


Forward-declare Wheel before Car.

@class Wheel;

@interface Car : ...

(BTW, in Wheel's -init method, parent is not initialized (thus always nil), therefore calling [parent doCarStuff] there is useless.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜