Implementing class: Cannot find superclass - iphone

I have ForceGaugeViewController class and ForceGaugeController class. I'm trying to make the ForceGaugeController class be a subclass of ForceGaugeViewController, but I'm receiving errors.
Error:
Cannot find interface declaration for ForceGaugeViewController
superclass of ForceGaugeController class.
ForceGaugeViewController.h
#import <UIKit/UIKit.h>
#import <math.h>
#import "HardwareController.h"
#import "ForceGaugeController.h"
#interface ForceGaugeViewController : UIViewController{
}
end
ForceGaugeViewController.m
#import "ForceGaugeViewController.h"
#implementation ForceGaugeViewController
ForceGaugeController.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#class ForceGaugeViewController;
// error here
#interface ForceGaugeController : ForceGaugeViewController{
}
#end
ForceGaugeController.m
#import "ForceGaugeController.h"

You can not only forward reference a class that you will inherit from or a protocol that you will implement. You just need to import the superclass in the header which you are already doing and then remove the #class declaration.
Edit: Also the superclass ForceGaugeViewController should not include the subclass ForceGaugeViewController in the header file.
ForceGaugeController.h
#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#interface ForceGaugeController : ForceGaugeViewController{
}
#end

Be sure to avoid circular imports:
Make sure you do not import any of the header or implementation subclasses files into the superclass file where the superclass interface declaration is (.h file)

You're including ForceGaugeController.h in ForceGaugeViewController.h
and including ForceGaugeViewController.h in ForceGaugeController.h. Circular includes will confuse the compiler and that's probably what's wrong.
A header file for a class only needs to include the framework (ie. UIKit), the subclass, and any protocols the class conforms to. Forward declarations will do for the classes of instance instance variables/method arguments/properties.

Remove the line:
#class ForceGaugeViewController;
You are already importing the ForceGaugeViewController class via its header file at the top of your ForceGaugeController.m file.
EDIT 1
And as pointed out by #Chris Devereux, you have a circular reference in your header files, you will want to get rid of that as well.
EDIT 2
And not sure how I missed the self-import in your ForceGaugeController.h file. I assume that it's a typo, but if not I'm sure you know you have to remove it.

Related

How to import all subclasses of a class at one time, in Objective-C? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
cannot find interface declaration for ‘AbstractPickerView’,superclass of ‘AttackLayer’
There are 3 header files (a superclass and 2 subclasses):
// A.h
#interface A : NSObject
#end
// A1.h
#import "A.h"
#interface A1 : A
#end
// A2.h
#import "A.h"
#interface A2 : A
#end
I want to import all subclasses of class A.
So I added 2 #import in A.h like this:
// A.h
#import "A1.h" // added
#import "A2.h" // added
#interface A : NSObject
#end
But after adding them, the following compile errors occurred.
Cannot find interface declaration for 'A', superclas of 'A1'
Cannot find interface declaration for 'A', superclas of 'A2'
I know that this error can be solved by createing and importing the following file.
// ASubclasses.h
#import "A1.h"
#import "A2.h"
But I think #import A.h is more simple than #import ASubclasses.h to import subclasses.
And if I can use #import A.h to import subclasses, I don't need to create another file(ASubclasses.h).
Is there a way to use #import A.h to import all subclasses of A?
Or in general, how do you import all subclasses of a class?
The error you're getting is because of an #import cycle . This happens when two header files import each other . You can just declare the class using #class and then import the headerfile in the implementation file.

Cannot find protocol declaration for

I have two objects, both of which are view controllers. The first (Ill call it viewController1) declares a protocol. The second (which unsurprisingly I will name viewController2) conforms to this protocol.
XCode is giving me a build error of: 'Cannot find protocol declaration for viewController1'
I have seen various questions on this subject and I am certain it is to do with a loop error, but I just can't see it in my case...
Code below..
viewController1.h
#protocol viewController1Delegate;
#import "viewController2.h"
#interface viewController1 {
}
#end
#protocol viewController1Delegate <NSObject>
// Some methods
#end
viewController2.h
#import "viewController1.h"
#interface viewController2 <viewController1Delegate> {
}
#end
Initially, I had the import line in viewController1 above that of the protocol declaration. This was preventing the project from building at all. After searching on SO, I realised the problem and switched the two lines around. I am now getting a warning (as opposed to an error). The project builds fine and actually runs perfectly. But I still feel there must be something wrong to be given a warning.
Now, as far as I can see, when the compiler gets to viewController1.h, the first thing it sees is the declaration of the protocol. It then imports the viewController.h file and sees this implements this protocol.
If it were compiling them the other way around, it would look at viewController2.h first, and the first thing it would do is import viewController1.h the first line of which is the protocol declaration.
Am I missing something?
Remove this line from viewController1.h:
#import "viewController2.h"
The problem is that viewController2's interface is preprocessed before the protocol declaration.
The general structure of the file should be like this:
#protocol viewController1Delegate;
#class viewController2;
#interface viewController1
#end
#protocol viewController1Delegate <NSObject>
#end
A.h:
#import "B.h" // A
#class A;
#protocol Delegate_A
(method....)
#end
#interface ViewController : A
#property(nonatomic,strong)id<ViewControllerDelegate> preViewController_B;(protocol A)
#end
B.h:
#import "A.h" // A
#class B;
#protocol Delegate_B
(method....)
#end
#interface ViewController : B
#property(nonatomic,strong)id<ViewControllerDelegate> preViewController_A;(protocol B)
#end
A.m:
#interface A ()<preViewController_B>
#end
#implementation A
(implement protocol....)
end
B.m:
#interface B ()<preViewController_A>
#end
#implementation B
(implement protocol....)
#end
For those who might need it:
It's also possible to fix this by moving the importation of ViewController1.h in ViewController2's implementation file (.m) instead of the header file (.h).
Like so:
ViewController1.h
#import ViewController2.h
#interface ViewController1 : UIViewController <ViewController2Delegate>
#end
ViewController2.h
#protocol ViewController2Delegate;
#interface ViewController2
#end
ViewController2.m
#import ViewController2.h
#import ViewController1.h
#implementation ViewController2
#end
This will fix the case where the error happens because ViewController1.h is imported in ViewController2.h before the protocol declaration.

Protocol error. Cannot find protocol declaration

We have error: cannot find protocol declaration for 'ClassWhichUseMainScene' [3]
We created file:
Protocol.h
#import "ScoreSystem.h"
#import "OtherSystem"
#import "OtherSystem2"
#class ScoreSystem;
#protocol SceneDelegate <NSObject>
#property (nonatomic, readonly,retain) ScoreSystem* score;
#property (nonatomic, readonly,retain) OtherSystem* system;
#property (nonatomic, readonly,retain) OtherSystem2* system2;
#end
And use in ScoreSystem.h
#import "Protocol.h"
#import "OtherSystem"
#import "OtherSystem2"
#interface ScoreSystem: NSObject <SceneDelegate>
{
OtherSystem* system;
OtherSystem2* system2;
}
In ScoreSystem we want use just OtherSystem and OtherSystem2 objects. In OtherSystem use ScoreSystem and OtherSystem2, etc.
We want create universal protocol for all system.
You have a circular dependency between your two header files (each imports the other). Do not import ScoreSystem.h in Protocol.h, the #class forward declaration is enough. The same goes for your other two imports.
As a general rule I avoid including class header files in other class header files - I just use #class everywhere and import the headers in the implementation files.
Mine Problem resolved with simple import statement
#import "ProtocolContainingFile.h"

Objective-C expected specifier-qualifier-list before

I've searched but I did not get with the solution. I have a class which inherits from NSObject. It has the following imports:
#import <Foundation/Foundation.h>
#import "Constants.h"
#import "CommonProtocols.h"
#import "SomeClass.h"
#interface SomeComtroller : NSObject
Than I have SomeClass.
#import "SomeController.h"
#interface SomeClass : NSObject
{
SomeController *myController; // ERROR!
}
#end
However, I can't define SomeController in SomeClass. It gives me the error, I really want to have a property of SomeController in SomeClass, and have in the controller a property of someClass.
You have a circular dependency — SomeClass.h imports SomeController.h, which imports SomeClass.h, which imports…
The solution is to move the imports into the implementation files, and just forward-declare the other classes you're using with the #class directive (e.g. #class SomeController; in SomeClass.h).

Adding C++ Object to Objective-C Class

I'm trying to mix C++ and Objective-C, I've made it most of the way but would like to have a single interface class between the Objective-C and C++ code. Therefore I would like to have a persistent C++ object in the ViewController interface.
This fails by forbidding the declaration of 'myCppFile' with no type:
#import <UIKit/UIKit.h>
#import "GLView.h"
#import "myCppFile.h"
#interface GLViewController : UIViewController <GLViewDelegate>
{
myCppFile cppobject;
}
#end
However this works just fine in the .mm implementation file (It doesn't work because I want cppobject to persist between calls)
#import "myCppFile.h"
#implementation GLViewController
- (void)drawView:(UIView *)theView
{
myCppFile cppobject;
cppobject.draw();
}
You should use opaque pointers and only include C++ headers in the file that implements your Objective-C class. That way you don't force other files that include the header to use Objective-C++:
// header:
#import <UIKit/UIKit.h>
#import "GLView.h"
struct Opaque;
#interface GLViewController : UIViewController <GLViewDelegate>
{
struct Opaque* opaque;
}
// ...
#end
// source file:
#import "myCppFile.h"
struct Opaque {
myCppFile cppobject;
};
#implementation GLViewController
// ... create opaque member on initialization
- (void)foo
{
opaque->cppobject.doSomething();
}
#end
Make sure that all files that include GLViewController.h are Objective-C++ sources (*.mm).
When you include C++ code in the header of your view controller, all sources that import this header must be able to understand it, so they must be in Objective-C++
You need to declare the C++ objects in an interface block in your .mm file.
In .mm:
#include "SomeCPPclass.h"
#interface SomeDetailViewController () {
SomeCPPclass* _ipcamera;
}
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)blabla;
#end
I think you need to set the following flag to true in your project settings:
GCC_OBJC_CALL_CXX_CDTORS = YES
This should allow you to instantiate C++ objects in your Objective-C classes.