I am working on an iPhone game using the cocos2d framework, and recently encountered a problem I can’t seem to solve. After adding a new class to my project and doing some work in it, I tried to compile my code and several of the classes I hadn’t even touched broke. It seems like those classes just forgot how import external classes. I am getting the error up there in several of my classes, but here is one example:
#import "cocos2d.h"
#import "XZombie.h"
#import "WayPoint.h"
#import "XBuilding.h"
//@class XBuilding;
@interface Hoarde : CCNode
{
float _spawnRate;
int _totalXombies;
NSMutableArray *_path;
XBuilding *_target;
NSMutableArray *_zombies;
bool _zombiesReset;
}
@property (nonatomic) float spawnRate;
@property (nonatomic) int totalXombies;
@property (nonatomic, retain) NSMutableArray *path;
@property (nonatomic, retain) XBuilding *target;
@property (nonatomic, retain) NSMutableArray *zombies;
@property (nonatomic, assign) bool zombiesReset;
- (id) initWithZombieCount:(int)totalXombies Target:(XBuilding *) target SpawnRate:(float)spawnrate;
- (void) resetZombies;
@end
I get the error on the line that reads XBuilding *_target;
If I uncomment the @class XBuilding;, the error goes away, so while that doesn’t really solve my problem, it gives me a tool to work around it.
If I do the @class trick for all the files I am having a problem with, I can work around that. The thing is, I get a new – but similar – error besides the specifier-qualifier-list one. Some lines of code give me Expected a ')' before *token or Expected a ';' before *token. Those lines usually gave me the previous error as well, so the @class trick worked as well, but I haven’t the slightest idea why this stuff is doing what it’s doing. I read somewhere (the cocos2d forums, I think) that renaming the .m files to .mm might do the trick, but it didn’t for me.
So, while I can continue working on my project, I would really like to know how on Earth to avoid stuff like that in the future…
Please check whether there is circular dependency, for example, importing
ClassB.hinClassA.hand vice versa. This is the typical scenario where “Expected specifier-qualifier-list before…” error occurs.I suggest to use only
@classin your headers files except:#importing the super class#importing the protocols your class implementsThe header files should be
#imported in your implementation files.“The Objective-C Programming Language” says:
More reference: