views:

32

answers:

2

Hi,

why isn't it possible in Cocoa that two Classes both import each other? I tried the following code:

Controller.h:

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

@interface Controller : NSObject {
 Model *model;
}

@end

Model.h:

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

@interface Model : NSObject {
 Controller *controller;
}

@end

which raises the following exceptions:

error: expected specifier-qualifier-list before 'Controller'
error: expected specifier-qualifier-list before 'Model'

Can someone please explain why this is?

Thanks! xonic

A: 

A solution for this is that: Model.h:

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

@class Controller;
@interface Model : NSObject {
 Controller *controller;
}

@end

And you done with that

vodkhang
A: 

Explain why? No.

But the solution is to use the @class declaration like so:

@class Model;
@interface Controller : NSObject {
 Model *model;
}
@end
Paul Lynch