如何正确处理Cocoa Touch(Objective-C cross C++)中的前向声明/#import?
我正在尝试编写这个头文件:
//@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的正常方法是:
您看到的重载错误可能是因为编译器尝试将
AQPlayer.h
解析为 Objective-C 而不是 Objective-C++。 您可能必须对导入 AQPlayer 的所有源文件使用.mm
,即使该特定类不使用 C++。The normal way to do this would be:
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++.我已经将 AQ_PWN_iPhoneViewController 设为 .mm 文件。 所以不,这并不能解决问题。
这里有更多信息:如果我尝试调用 AQPlayer 实例的方法/函数,将显示错误“错误:找不到'AQPlayer'的接口声明”。 这是一个示例:
<代码>
- (void)viewDidLoad {
}
删除
像 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 {
}
Removing any function call like StartQueue will make the error go away, but I do need to call the method!
我也遇到了完全相同的问题(也尝试重用 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"
首先,您必须告诉编译器一个文件具有 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.