-2

I'm getting this error,

Unknown type name ArrowWrapper

from within BoxSprite.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "BoxSum.h"
#import "ArrowWrapper.h"

@interface BoxSprite : CCSprite {

}
@property ArrowWrapper* arrowItem;
@end

Also, ArrowWrapper.h contains this.

#import "cocos2d.h"
#import "BoxSprite.h"

@interface ArrowWrapper : CCMenuItem {

}

@property BoxSprite* box;
@end

The error used to be in ArrowWrapper saying it couldnt find BoxSprite until I did a clean, and now it's in BoxSprite saying it can't find ArrowWrapper.

I can't figure out what I'm missing.

Thanks in advance for any help.

Austin
  • 4,801
  • 6
  • 34
  • 54

2 Answers2

2

You have a recursive import: "BoxSprite.h" imports "ArrowWrapper.h" and vice versa.

You have to remove one of the import statements and use @class instead. For example in "BoxSprite.h" replace

#import "ArrowWrapper.h"

by

@class ArrowWrapper;

You can then import "ArrowWrapper.h" in the implementation file "BoxSprite.m", if necessary.

Detailed explanation: Xcode displays the error in "BoxSprite.h", but the error actually occurs when "ArrowWrapper.m" is compiled:

  1. "ArrowWrapper.m" imports "ArrowWrapper.h".
  2. "ArrowWrapper.h" imports "BoxSprite.h" before defining the ArrowWrapper class.
  3. "BoxSprite.h" imports "ArrowWrapper.h": But "ArrowWrapper.h" is already marked as imported, so the compiler does not read it again.
  4. Therefore, when reading "BoxSprite.h", the ArrowWrapper class has not been defined yet, causing the compiler error.

Replacing import by @class solves the problem, because it makes the ArrowWrapper class known to the compiler at that point without reading the interface file.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    @Austin: `@class` makes the class known to the compiler without actually importing the interface file. It is a common method to resolve #import "loops". – Martin R Sep 29 '12 at 11:33
  • @Austin: See for example http://stackoverflow.com/questions/322597/class-vs-import for more details. – Martin R Sep 29 '12 at 11:34
  • @Austin: See also my addition to the answer, it should explain the problem and solution in your case. – Martin R Sep 29 '12 at 11:52
0

I think the issue is with import statements.

You are importing #import "ArrowWrapper.h" in BoxSprite.h and importing #import "BoxSprite.h" in ArrowWrapper.h

So Change the BoxSprite.h like :

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "BoxSum.h"

@class ArrowWrapper;
@interface BoxSprite : CCSprite {

}
@property ArrowWrapper* arrowItem;
@end
Midhun MP
  • 103,496
  • 31
  • 153
  • 200