2

I'm working on a Cocos2D and Box2D game. I just created a class for the game interface so players can choose different control types. Because I'm using Box2D for the physics seemingly all the classes must be .mm instead of .m - but when I go to define the new controller object in HelloWorldLayer.h I get the following errors:

"ISO C++ Forbids the declaration of 'SparkController' with no type". and "Expected ';' before '*' token".

I found several articles where people had gotten a similar error but all were very different situations. I get the sense this is one of those generic errors that could be caused by a lot of different issues.

The declaration of my SparkController instance in HelloWorldLayer.h looks as follows. This is where the errors pop up:

SparkController *_controller;

So it seeems like it's trying to parse this line as C++ code? The class SparkController.h and SparkController.mm are both written entirely in Objective-C, I'm not even including Box2D in the class because it's not needed. To see if the .mm was the issue, I tried changing SparkController.mm to .m and the compiler threw 200 errors before giving up. So I have no idea what I might be doing wrong. What other code would it be useful for me to post here to help diagnose the issue?

EDIT: Thanks for all the comments below. Unfortunately none of your suggestions worked. I'm including the header file for SparkController.h, maybe it will help point out what I've done wrong:

#import "cocos2d.h"
#import "HelloWorldLayer.h"

@interface SparkController : NSObject {
    BOOL _drawPreviewLine;
    CGPoint _touchStartLocation;
    float _previewAngle;
    float _sparkAngle;
    CCParticleSystemQuad *_spark;
}

@property (nonatomic, assign) BOOL drawPreviewLine;
@property (nonatomic, assign) CGPoint touchStartLocation;
@property (nonatomic, assign) float previewAngle;
@property (nonatomic, assign) float sparkAngle;
@property (nonatomic, retain) CCLayer *layer;

//+(id)initWithLayer:(CCLayer *)layer;

@end

@interface GestureController : SparkController{

}
+(id)initWithLayer:(CCLayer *)layer;
-(void)touchBeganAt:(CGPoint)touchStartLocation;
-(void)touchMovedTo:(CGPoint)touchLocation;
-(void)touchEndedAt:(CGPoint)touchLocation;

@end

@interface HybridController : SparkController{

}
+(id)initWithLayer:(CCLayer *)layer;
@end

@interface TouchController : SparkController{

}
+(id)initWithLayer:(CCLayer *)layer;
@end

Here's the relevant part of HelloWorldLayer.h if it helps:

#import "cocos2d.h"
#import "Box2D.h"
#import "MyContactListener.h"
#import "GLES-Render.h"
#import "SparkController.h"

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{

...

    SparkController *_controller;   

}
James
  • 736
  • 1
  • 5
  • 19

3 Answers3

2

It's telling you that it doesn't recognize SparkController. It seems like the error could stem from the line above or maybe you need class SparkController; at the top of HelloWorldLayer.h.

EDIT: Your problem is you are importing HelloWorldLayer.h in SparkController and SparkController.h in HelloWorldLayer. This will not work. You should import those in the implementation (.m) files instead and forward declare classes in the .h files.

HelloWorldLayer.h: @class SparkController;

HelloWorldLayer.m: #import "SparkController.h"

SparkController.h: @class HelloWorldLayer;

SparkController.m: #import "HelloWorldLayer.h"

vakio
  • 3,134
  • 1
  • 22
  • 46
  • Is it `class SparkController;` or `@class SparkController;` ? – Ilanchezhian Feb 02 '12 at 08:47
  • In C++ it's class not \@class. But I think @class should work with C++ classes as well. Either way, even if you use class/@class your implementation file (.mm) still needs to #import the header file in question. – CodeSmile Feb 02 '12 at 10:08
  • I tried adding \@class SparkController and class SparkController to the top. Adding \@class gave no errors itself but didn't fix the error above. Adding it without the at gave an error because it conflicted with the line below, \@interface : SparkController : NSObject etc. – James Feb 02 '12 at 23:15
  • 1
    See my edit. You are causing a recursive import in your two classes. – vakio Feb 03 '12 at 09:20
  • Awesome - this sounds like the ticket - thanks for your help :-) – James Feb 03 '12 at 20:15
1

If you use the .mm file extension, Xcode assumes that your code is Objective-C++, thus the C++ error message. Regarding the error message itself, the compiler complains that it does not know anything about SparkController, i.e. it has not seen a type declaration before parsing the line of code you have given. Maybe you forgot to include the right header file.

swegi
  • 4,046
  • 1
  • 26
  • 45
0

Check for @class and #import presence in your HelloWorldLayer files.

Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • I'm not sure where/how to add @class. All objective-c class declarations I've seen haven't had it. Should it go in .h or .mm? Can you provide an example? Thanks! – James Feb 03 '12 at 00:57
  • Add it directly to the top of your .h file. Edit: Look at this question, the answer is comprehensive: http://stackoverflow.com/questions/322597/class-vs-import – Kheldar Feb 03 '12 at 09:34