2

My .h file:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameData.h"
#import "PROBattleScene.h"

@interface PROBattleAI : NSObject {
    BattleType type;
    PROBattleScene *scene;
}

-(id)initWithType:(BattleType)_type andBattleInformation:(NSMutableDictionary*)_information andScene:(PROBattleScene*)_scene;
-(void)dealloc;
@end

But on the line PROBattleScene *scene; I get the unknown type name error from Xcode.

I tried the answer here: xcode unknown type name but I am already doing that (and doesn't work).

Why is that happening? I am already importing my PROBattleScene.h file, why isn't it being recognized?

EDIT: And the contents of PROBattleScene.h as requested:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameData.h"
#import "SimpleAudioEngine.h"

#import "PROBattleBackground.h"
#import "PROBattleAI.h"

@interface PROBattleScene : CCLayer {
    NSMutableDictionary *battleInformation;
    NSMutableArray *localPlayerPartyData;

    PROBattleBackground *background;

    CCNode *base;

    PROBattleAI *enemyAI;
}
+(CCScene*)scene;
-(id)init;
-(void)loadBattleInformation;
-(void)loadBGM;
-(void)loadBackground;
-(void)loadBase;
-(void)loadEnemyAI;
-(void)beginBattle;

@end
Community
  • 1
  • 1
Saturn
  • 17,888
  • 49
  • 145
  • 271

1 Answers1

7

You have a circular dependency. PROBattleAI imports PROBattleScene which imports PROBattleAI which imports PROBattleScene < zomg infinite loop >

Use @class PROBattleWhatever in your headers as much as possible. Only import headers for protocol definitions or superclasses.

EDIT Ok, the above wording was totally bad...and misleading. Here is what (I believe) happens in detail. Your PROBattleAI imports PROBattleScene, which then imports PROBattleAI, which then imports PROBattleScene for a second time (all before it gets to any of the code in either file). The import will ignore PROBattleScene this time because it has already been imported and you will get the undefined type error since the file was skipped.

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • `#import` prevents this loop, it is `#include` which creates this infinite inclusion loop. check this out : http://stackoverflow.com/questions/439662/what-is-the-difference-between-import-and-include-in-objective-c – Saurabh Passolia Jul 21 '12 at 17:49
  • @samfisher #import prevents it from being imported more than once (like C++ #ifndef logic) but it doesn't work well against files the #import each other. – borrrden Jul 21 '12 at 17:51
  • Ah, I see. Didn't see that coming. – Saturn Jul 21 '12 at 17:52
  • @samfisher I will admit my poor wording though, see my updated answer :). – borrrden Jul 21 '12 at 18:00