Compiler won't recognize my custom class - objective-c

Here is my header file for Board:
#import "Game.h"
#import <Foundation/Foundation.h>
#interface Board : UIView
{
enum Piece;
}
- (void) setGame: (Game*) theGame; //<-- this is where the error is
typedef enum {X, O, NONE} Piece;
- (float)getSection;
#end
The compiler says "Expected a type" and has (Game*) underlined. What is the problem here?
Game.h:
#import <Foundation/Foundation.h>
#import "Board.h"
#interface Game : UIViewController
- (void)boardwasTapped:(int) row:(int) column;
#end

Don't import your header, but forward declare it. Import the Game header in Board.m
#class Game;
#interface Board : UIView {
...
}
...
#end
Also, are you sure the problem isn't with your enum? You are using it in your header before you declare it. You should declare it above (outside) the #interface block.

Board import game, and Game import Board. So you need to forward class Game in Board
#Class Game

Related

"Attempting to use the forward class 'Game' as superclass of 'MathGame'" in Cocos2d

I'm making a Cocos2d game for iphone, and I have my main game mode, Game, which inherits from CCLayer.
I'm trying to make another game mode, MathGame, which inherits from Game, but when I try to compile, I get this error in MathGame.h:
Attempting to use the forward class 'Game' as superclass of 'MathGame'
I get the error even if the implementation and interface of MathGame are empty. And it only happens if I try to include MathGame.h in another file.
Here's the code for the Game class:
// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#import "SplashScreenLayer.h"
#interface Game : CCLayer
// A bunch of stuff
#end
The new game type:
// MathGame.h
#import "Game.h"
#interface MathGame : Game
#end
And the main menu that includes both:
// SplashScreen.h
#import "cocos2d.h"
#import "Game.h"
#import "MathGame.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"
#interface SplashScreenLayer : CCLayer
// A bunch of stuff
#end
I can't find anything helpful online. Any ideas?
You simply have an import cycle:
Game imports SplashScreenLayer
SplashScreenLayer imports MathGame
MathGame imports Game
Your solution:
Leave the import inside the MathGame, and change the other imports to #class.
To sum it up:
// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#class SplashScreenLayer;
#interface Game : CCLayer
// A bunch of stuff
#end
The new game type:
// MathGame.h
#import "Game.h"
#interface MathGame : Game
#end
And the main menu that includes both:
// SplashScreen.h
#import "cocos2d.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"
#class Game;
#class MathGame;
#interface SplashScreenLayer : CCLayer
// A bunch of stuff
#end
With your question answered above, let me explain a few things I already know from reading about forward declerations and import cycles:
First, go read about them! They are a very important part of Objective-C, and you don't want to miss it!
Secondly, use #class whenever you need that class for private variables or method parameters. Use imports for inheritance and strong properties.
Thirdly, don't forget to #import your forwarded classes in the implementation file!
In my case,I user the xx class and use the #class but not #import the .h file.and the compile complain..

objective-c iso C++ forbids declaration of X with no type

i'm having some weird compiler errors in objective-c:
iBody.h:18: error: ISO C++ forbids declaration of 'iObject' with no type
iObject.h
#import "iElement.h"
#import "CCSprite.h"
#import "iBody.h"
#interface iObject : iElement
{
iBody *body;
}
-(iObject*)initElement:(CGPoint)pos
withName:(NSString*)name
zIndex:(NSInteger)z
withImage:(NSString*)image;
-(void) addBody: (iBody*) body;
-(iBody*) getBody;
#end
iBody.h
#import "iObject.h"
#import "b2Body.h"
#interface iBody : NSObject
{
CGPoint position;
float angle;
b2Body *body;
iObject *parent;
}
-(iBody*) initElement: (CGPoint) pos
withAngle: (float) angle
withParent: (iObject*) el;
-(void) setBody: (b2Body*)bdy;
-(iObject*) getParent;
#end
can someone please explain why this is happening and how to fix it. The implementation of the classes have .mm extension.
Thanks!
This is because you have a circle include of header files.
See, you are including iBody.h in iObject.h and 'iObject.h' in iBody.h. So the compiler will see something like this:
#interface iBody : NSObject
{
CGPoint position;
float angle;
b2Body *body;
iObject *parent;
}
-(iBody*) initElement: (CGPoint) pos
withAngle: (float) angle
withParent: (iObject*) el;
-(void) setBody: (b2Body*)bdy;
-(iObject*) getParent;
#end
#interface iObject : iElement
{
iBody *body;
}
-(iObject*)initElement:(CGPoint)pos
withName:(NSString*)name
zIndex:(NSInteger)z
withImage:(NSString*)image;
-(void) addBody: (iBody*) body;
-(iBody*) getBody;
#end
As you can see iBody doesn't know about iObject when it is declined.
To resolve such situation you should just add string #class iObject; before #interface iBody: NSObject and remove include of iObject.h in iBody.h. But in implementation file iBody.m you should import iObject.h
It seems like you're in an import loop, since the headers of iBody and iObject are linking to one another. Typically in this situation I would consider the iObject to be of higher status and use the following in iBody.h:
// Replace the import to iObject.h with this:
#class iObject;
// Add the import back in iBody.mm
#import "iObject.h"
Now there is no import loop and only the implementation file of iBody actually links to iObject.h, and since no headers link to iBody.mm the issue is solved. Also, remember to rename the implementation files to .mm (Obj-C/C++) when working with Box2D, that gets me now and then :)

Objective-C type not found

I'm trying to compile my Objective-C application but it fails saying "Unknown type name 'OpenGLView'". Now I know what this means but the weird thing is I'm convinced I'm importing it just fine.
#import <Foundation/Foundation.h>
#import "OpenGLView.h"
#import "Frame.h"
#import "Mesh2.h"
#import "Vector2.h"
#import "StaticRigidBody.h"
#import "DynamicRigidBody.h"
#interface PhysicsApplicationController : NSObject {
Frame* frame;
OpenGLView* view;
}
It fails on line 11 of this sample. You can see I import it on line 2. The thing which I can't get my head around though is the same import works completely fine in this segment.
#import <UIKit/UIKit.h>
#import "OpenGLView.h"
#interface PhysicsAppDelegate : UIResponder <UIApplicationDelegate> {
OpenGLView* _glView;
}
Both source files for the two segments are in the same directory. I have no idea what is going on.
[EDIT]
Content of OpenGLView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#import "PhysicsApplicationController.h"
#interface OpenGLView : UIView {
CAEAGLLayer* _eaglLayer;
EAGLContext* _context;
GLuint _colorRenderBuffer;
GLuint _positionSlot;
GLuint _colorSlot;
PhysicsApplicationController* _physicsController;
}
typedef struct {
float position[3];
float color[4];
} Vertex;
- (void)renderFrame:(Frame*)frame;
- (void)drawObjectsInFrame:(Frame*)frame;
- (void)setupVBOsWithVertices:(Vertex*)vertices Indices:(GLubyte*)indices;
- (void)setPhysicsController:(PhysicsApplicationController*)controller;
#end

#import in objective C: Am I doing this wrong?

Sorry, couldn't find a more appropriate title.
In My code I have two classes which should know of each others existence. So I use an instance variable which points to the other class. For that to work (I guess?) the other classes headers file should be imported so it knows which methods it has and such.
Here is my code (stripped down)
MainMenuController.h:
#import <Cocoa/Cocoa.h>
#import "IRCConnection.h"
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection;
}
#property (strong) IRCConnection *ircConnection;
#end
IRCConnection.h:
#import <Foundation/Foundation.h>
#import "MainMenuController.h"
#interface IRCConnection : NSObject {
MainMenuController *mainMenuController;
}
#property (strong) MainMenuController *mainMenuController;
#end
As you can see they both import each other, but this creates an error (Unknown type name 'IRCConnection') in one, and in the other Unknown type name 'MainMenuController'.
However when the connection is just one way (e.g. only MainMenuController knows about IRCConnection) and thus there is only an import statement in one of the two, it works fine.
How can I have them to know about each other? In both ways.
Hope this question makes any sense.
you could remove the import from IRCConnection.h and use a #class statement instead.
like this:
#import <Foundation/Foundation.h>
#class MainMenuController;
#interface IRCConnection : NSObject {
then add a #import "MainMenuController.h" to IRCConnection.m
In the header, use forward declaration:
#class IRCConnection;
#interface MainMenuController : NSViewController {
IRCConnection *ircConnection; // ok
}
In the source file (.m), do #import.
You cannot have circular imports. You need to break them up, or introduce some forward declarations.

#protocol implementation in #interface in Objective-C

I need to develop an application which has a interface which implements methods of 3 protocols.
Assume protocol A extends protocol B and protocol C, and interface implements protocol A.
This is how my code looks,
// This is in MyClass.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_A"
#interface MyClass : NSObject <protocol_A>
{
}
#end
//This is MyClass.m file
#import "MyClass.h"
#implementation myClass
-(void)methodinA
{
NSLog(#"I'm in protocol_A");
}
}
-(void)methodinB
{
NSLog(#"I'm in protocol_B");
}
-(void)methodinC
{
NSLog(#"I'm in protocol_C");
}
#end
//This is protocol_A.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h"
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
//This is in protocol_B.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_B
-(void)methodinB;
#end
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
i'm getting an exception , and my app is getting crashed...
***Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyClass 0X323nm31>setvalue:forundefinedKey:]:this class is not key value coding-compilant for the key window'.
Plz Tel me how to solve this problem??
So where you're getting this from (and the reason you're getting it 3 times) is you've got a mistake in your protocol definitions. You have:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
{
}
-(void)methodinC;
#end
You can't declare class members in a protocol: only methods. Because of this, you don't need (and, as you've discovered) can't have the curly braces in the protocol definition. As such, you need this for your protocol definitions:
//This is in protocol_C.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#protocol protocol_C
-(void)methodinC;
#end
Removing those should solve your issue.
When making new files, I always go through Xcode's new-class-files process, as it frequently gives you lots of convenient stuff. Here is the contents of a new protocol_D declaration fresh from Xcode:
#import <Cocoa/Cocoa.h>
#protocol protocol_D
#end
Hope this helps!
TL;DR: Protocol definitions can't have curly-braces anywhere in them.
Protocols generally go in a .h file; always go in a .h file if you plan on using them anywhere.
Just like everything else, you need to #import the .h file that contains the definition of the protocol before you use it.
So, in MyClass.h (it really should be capitalized -- Classes are always capitalized in Objective-C), #import the various protocol .h files.
Your protocol_A.h file declares conformance to protocol_B and protocol_C, yet you haven't imported the headers for protocol_B and protocol_C. This means that you are declaring conformance to protocols that as far as the compiler is concerned, don't exist in protocol_A.h. You need to import the headers:
In protocol_A.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "protocol_B.h" //note these new imports
#import "protocol_C.h"
#protocol protocol_A <protocol_B, protocol_C>
-(void)methodinA;
#end
Also see Apple's Communicating with Objects, which discusses delegates, protocols, and selectors. Though its listed under Mac OS X, most (if not all) appears to apply to iOS also.