views:

3555

answers:

6

I am trying to write this header file:

//@class AQPlayer;

//#import "AQPlayer.h"

@interface AQ_PWN_iPhoneViewController : UIViewController {

    AQPlayer* player;
}

@end

AQPlayer is a .mm file written in C++.

I tried to make a class forward declaration here, but it complains to me:

error: cannot find interface declaration for 'AQPlayer'

So I tried to "#import" the header file instead, but it complains something completely off and weird. Here's a slice of the error complained:

In file included from

/Users/akaraphan/Desktop/SpecialTopic1/AQ_PWN_iPhone/Classes/AQPlayer.h:51,
                 from /Users/akaraphan/Desktop/SpecialTopic1/AQ_PWN_iPhone/Classes/AQ_PWN_iPhoneViewController.h:12,
                 from /Users/akaraphan/Desktop/SpecialTopic1/AQ_PWN_iPhone/Classes/AQ_PWN_iPhoneAppDelegate.m:10:
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:78: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CAStreamBasicDescription'
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:230: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:231: error: expected '=', ',', ';', 'asm' or '__attribute__' before '==' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:233: error: expected '=', ',', ';', 'asm' or '__attribute__' before '!=' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:234: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<=' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:235: error: expected '=', ',', ';', 'asm' or '__attribute__' before '>=' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:236: error: expected '=', ',', ';', 'asm' or '__attribute__' before '>' token
/Developer/Examples/CoreAudio/PublicUtility/CAStreamBasicDescription.h:239: error: expected ';', ',' or ')' before '&' token

Am I missing something? Can't I do a forward declaration for this?

+2  A: 

The normal way to do this would be:

// In your .h file...
@class AQPlayer;
@interface AQ_PWN_iPhoneViewController : UIViewController {
    AQPlayer *player;
}
@end

// In your .mm file (see below why it has to be .mm instead of .m)...
#import "AQ_PWN_iPhoneViewController.h"
#import "AQPlayer.h"
@implementation AQ_PWN_iPhoneViewController
...
@end

The heavy duty errors you see is probably because the compiler is trying to parse AQPlayer.h as Objective-C instead of Objective-C++. You'll probably have to use .mm for all of your source files that imports AQPlayer, even if that particular class doesn't use C++.

Daniel Dickison
+1  A: 
You should edit your original question instead of posting this as an answer.Having said that, as AQPlayer an obj-c or C++ class? The forward declaration you gave `@class AQPlayer;` is for Obj-C, but your method call `player->StartQueue(false);` is C++ syntax. Either you want `class AQPlayer;` for C++, or `[player startQueue:NO];` for Obj-C.
Daniel Dickison
A: 

Anybody? please.

A: 

I was also getting exactly the same issue (also trying to reuse the AQPlayer c++ class from the Apple "SpeakHere" demo).

Seems the compiler gets confused and tries to compile objective-c++ as objective-c (hence all the errors). I managed to get it to compile by right clicking on the m file that imports the mm file and selecting 'getInfo', then changing the filetype to "sourcecode.cpp.objcpp"

A: 

Firstly, you have to tell the compiler that a file has C++ code (a file including a file with C++ code also!). You can do this by renaming it to a .mm extension (.h stays the same). Second way is to set the file type to 'sourcecode.cpp.objcpp' like Atomoil said.

Also, if you want forward declaration for a C++ class you use 'class AQPlayer;', not '@class AQPlayer;'. If you do, you will get a 'cannot find interface declaration for XXX' error..

I usually include C++ files in the .h file, eliminating the need for forward declaration, although sometimes you don't want to do this.

StijnSpijker
A: 

This is how I solved it:

You need

  • the view
  • the viewcontroller
  • the controller

so:

  • the view references viewcontroller(owner) and controller(add a class to the nib, and reference the controller)
  • the viewcontroller has a field controller (must have the extensions .h and .m)
  • the controller must have the extensions .h and .mm

After all this, you can assign the visual elements to the fields in the controller class. The same thing with the action listeners.

The main reason (I think) is that you cannot mix the UIController with CPP code.

If you pay attention to the SpeakHere example, things are done this way.

Hope it helps :).

Lucas