如何正确处理Cocoa Touch(Objective-C cross C++)中的前向声明/#import?

发布于 2024-07-27 12:08:28 字数 1953 浏览 8 评论 0原文

我正在尝试编写这个头文件:

//@class AQPlayer;

//#import "AQPlayer.h"

@interface AQ_PWN_iPhoneViewController : UIViewController {

    AQPlayer* player;
}

@end

AQPlayer 是一个用 C++ 编写的 .mm 文件。

我尝试在这里进行类前向声明​​,但它向我抱怨:

错误:找不到“AQPlayer”的接口声明

所以我尝试“#import”头文件,但它抱怨一些完全关闭和奇怪的事情。 这是所抱怨的错误的一部分:

在包含的文件中

/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

我缺少什么吗? 我不能为此做一个前瞻性声明吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-08-03 12:08:28

执行此操作的正常方法是:

// 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

您看到的重载错误可能是因为编译器尝试将 AQPlayer.h 解析为 Objective-C 而不是 Objective-C++。 您可能必须对导入 AQPlayer 的所有源文件使用 .mm,即使该特定类不使用 C++。

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++.

小巷里的女流氓 2024-08-03 12:08:28

我已经将 AQ_PWN_iPhoneViewController 设为 .mm 文件。 所以不,这并不能解决问题。

这里有更多信息:如果我尝试调用 AQPlayer 实例的方法/函数,将显示错误“错误:找不到'AQPlayer'的接口声明”。 这是一个示例:

<代码>
- (void)viewDidLoad {

CFStringRef ref = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"1.mp3"];
player = new AQPlayer(&ref);

OSStatus result = player->StartQueue(false);

if (result == noErr)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playbackQueueResumed" object:self];

}
删除

像 StartQueue 这样的任何函数调用将使错误消失,但我确实需要调用该方法!

I already made my AQ_PWN_iPhoneViewController to be a .mm file. So no, that doesn't fix it.

here's some more info: The error "error: cannot find interface declaration for 'AQPlayer'" will be shown if I tried to call a method/function of the AQPlayer instance. Here's an example:


- (void)viewDidLoad {

CFStringRef ref = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"1.mp3"];
player = new AQPlayer(&ref);

OSStatus result = player->StartQueue(false);

if (result == noErr)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playbackQueueResumed" object:self];

}

Removing any function call like StartQueue will make the error go away, but I do need to call the method!

幽梦紫曦~ 2024-08-03 12:08:28

我也遇到了完全相同的问题(也尝试重用 Apple“SpeakHere”演示中的 AQPlayer c++ 类)。

编译器似乎感到困惑并尝试将 Objective-C++ 编译为 Objective-C (因此出现所有错误)。 我设法通过右键单击导入 mm 文件的 m 文件并选择“getInfo”,然后将文件类型更改为“sourcecode.cpp.objcpp”来编译它

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"

暗藏城府 2024-08-03 12:08:28

首先,您必须告诉编译器一个文件具有 C++ 代码(一个文件还包含一个具有 C++ 代码的文件!)。 您可以通过将其重命名为 .mm 扩展名(.h 保持不变)来完成此操作。 第二种方法是将文件类型设置为“sourcecode.cpp.objcpp”,就像 Atomoil 所说的那样。

另外,如果您想要 C++ 类的前向声明,请使用“class AQPlayer;”,而不是“@class AQPlayer;”。 如果这样做,您将收到“找不到 XXX 的接口声明”错误。

我通常在 .h 文件中包含 C++ 文件,从而消除了前向声明的需要,尽管有时您不想这样做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文