Adding C++ Object to Objective-C Class - iphone

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.

Related

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"

My #property is declared yet I still get may not respond to warning

I don't like warnings lying around and this one has been bothering me. Any ideas on what I am doing wrong? I have tons of properties using this same approach and none of them are giving me warnings. Why doesn't Xcode recognize this one?
While the app works as expected, Xcode gives me the following compile time warning:
'OnlinePeerBrowser' may not respond to '-setMyParent:'
My property declaration in OnlinePeerBrowser.h
#import "WelcomeViewController.h"
#interface OnlinePeerBrowser : UIViewController <UITableViewDelegate, UITableViewDataSource, NSNetServiceBrowserDelegate> {
WelcomeViewController *_myParent;
}
#property (nonatomic, assign) WelcomeViewController *myParent;
OnlinePeerBrowser.m has
#synthesize myParent=_myParent;
I am getting the warning on setMyParent in WelcomeViewController.m here...
#import "WelcomeViewController.h"
#import "OnlinePeerBrowser.h"
#implementation WelcomeViewController
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type {
...
OnlinePeerBrowser *controller = [[OnlinePeerBrowser alloc]
initWithNibName:#"OnlinePeerBrowser" bundle:nil];
[controller setMyParent:self];
}
Also, what is weird is that I can not use the dot syntax here either.
controller.myParent = self;
gives me the following error:
/Users/vesselhead/Development/iPhone/DJBox/WelcomeViewController.m:254: error: request for member 'myParent' in something not a structure or union
I feel like I must be missing something very simple.
The code you've posted looks correct. That means that the compiler is pulling in another declaration of the OnlinePeerBrowser class from somewhere.
Check for circular imports.
Check if you have multiple copies of the OnlinePeerBrowser.h file.
Add the line #warning Testing to your OnlinePeerBrowser.h file. That warning should then appear in the log when you compile. If that warning doesn't appear then that file isn't being picked up by the compiler.
If it's a circular import then don't import "WelcomeViewController.h" in "OnlinePeerBrowser.h". Instead, use a forward declaration in OnlinePeerBrowser.h, e.g. #class WelcomeViewController , and import "WelcomeViewController.h" in OnlinePeerBrowser.m
Sometimes Circular Imports create an issue with the compiler.
Instead of using
#import "WelcomeViewController.h"
in OnlinePeerBrowser.h move that line to the OnlinePeerBrowser.m and add
#class WelcomeViewController
to the OnlinePeerBrowser.h
this will allow you to set the Class of myParent and _myParent to WelcomeViewController and not have the Circular Import.
Alternatively:
you may want to use a #protocol that the WeclomeViewController would have to adhere to. Then you would only have to import the Classes in one direction.
the implementation for a Protocol property would be as Follows
//#import "WelcomeViewController.h"
#protocol OnlinePeerBrowserParent <NSObject>
#required
- (NSString*) informationFromParent;
#end
#interface OnlinePeerBrowser : UIViewController <UITableViewDelegate, UITableViewDataSource, NSNetServiceBrowserDelegate> {
id<OnlinePeerBrowserParent> _myParent;
}
#property (nonatomic, assign) id<OnlinePeerBrowserParent> myParent;
notice the Protocol is on the OnlinePeerBrowser.h so you can import the OnlinePeerBrowser.h and get the Protocol by default.
finally you implement the Protocol in the WelcomeViewController as so
#implementation WelcomeViewController<OnlinePeerBrowserParent>
- (NSString*) informationFromParent
{
return #"My Parental Info";
}
...... etc

Implementing class: Cannot find superclass

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.

Understanding forward declaration warning

I am writing my first lines in objective-c for an iPhone app.
This is the code:
/* ViewController.h */
#protocol ImageFlowScrollViewDelegate;
#interface ViewController : UIViewController<ImageFlowScrollViewDelegate> {
NSMutableArray *characters;
UILabel *actorName;
}
/* ViewController.m */
#import "ImageFlowScrollView.h"
#implementation IMDBViewController
/* methods here */
/* ImageFlowScrollView.h */
#protocol ImageFlowScrollViewDelegate;
#interface ImageFlowScrollView : UIScrollView<UIScrollViewDelegate> {
NSMutableArray *buttonsArray;
id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;
}
#property(nonatomic, assign)id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;
- (id)initWithFrame:(CGRect)frame imageArray:(NSArray *) anArray;
- (void)focusImageAtIndex:(NSInteger) index;
#end
#protocol ImageFlowScrollViewDelegate<NSObject>
#optional
- (void)imageFlow:(ImageFlowScrollView *)sender didFocusObjectAtIndex: (NSInteger) index;
- (void)imageFlow:(ImageFlowScrollView *)sender didSelectObjectAtIndex: (NSInteger) index;
#end
Doing this, I get a
warning: no definition of protocol
'ImageFlowScrollViewDelegate' is found
I can fix it using:
#import "ImageFlowScrollView.h"
#interface IMDBViewController : UIViewController<ImageFlowScrollViewDelegate> {
NSMutableArray *characters;
UILabel *actorName;
}
but I would like to know why the forward declaration approach is giving me a warning.
The forward declaration defines the symbol so the parser can accept it. But when you try to use the protocol (or class) - as you do by conforming to the protocol - the compiler needs it's definition to know the layout and size of the resulting object.
In addition, you can forward a class or protocol when you are only using it in the class (in an ivar for example). The compiler then only needs to know the existence of the symbol. But when making use of the class (in the implementation file), the methods need to be declared before use hence the need to include the declaration.
For example :
/* AViewController.h */
#class AnotherClass;
#interface AViewController : UIViewController {
AnotherClass* aClass; //only need the declaration of the name
}
#end
/* AViewController.m */
#import "AnotherClass.h"
#implementation AViewController
- (void) useAnotherClass {
[AnotherClass aMessage]; //aMessage needs to be declared somewhere, hence the import
}
#end
In addition, you already know that you must provide actual implementations in order to link your program.