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;
}
It’s telling you that it doesn’t recognize
SparkController. It seems like the error could stem from the line above or maybe you needclass 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"