0

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...

Hailei
  • 42,163
  • 6
  • 44
  • 69
Argent
  • 795
  • 2
  • 7
  • 25

1 Answers1

1

Please check whether there is circular dependency, for example, importing ClassB.h in ClassA.h and vice versa. This is the typical scenario where "Expected specifier-qualifier-list before..." error occurs.

I suggest to use only @class in your headers files except:

  • #importing the super class
  • #importing the protocols your class implements

The header files should be #imported in your implementation files.

"The Objective-C Programming Language" says:

The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.

More reference:

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • I worked my way around that, kind of hacking it all together with `@class`es and `#import`s. I kind of forgot to reply to this... but it helped. – Argent May 02 '12 at 11:06