在 Objective C 中通过引用传递原始数据类型
我有一个从 Objective CI 调用的 C++ 函数,需要通过引用 C++ 函数来传递变量。但是我在 xcode 中收到以下错误 - “在 '&' 之前预期有 ';'、',' 或 ')' foo.h 中的令牌
“foo.h”中的函数声明
#ifdef __cplusplus
extern "C"
{
#endif
NSString * LLtoUTM(double Lat,double Long,double &UTMNorthing, double &UTMEasting);
#ifdef __cplusplus
}
#endif
test_viewcontroller.m 中的函数调用
double UTM_x;
double UTM_y;
UTMzone = [[NSString alloc] init];
UTMzone = (NSString *) LLtoUTM(latitude,longitude,UTM_y,UTM_x);
谁能告诉我出了什么问题吗?
I have a C++ function which I call from Objective C.I need to pass variables by reference to the C++ function.But I get the following error in xcode - "Expected ';', ',' or ')' before '&' token in foo.h"
Function declaration in "foo.h"
#ifdef __cplusplus
extern "C"
{
#endif
NSString * LLtoUTM(double Lat,double Long,double &UTMNorthing, double &UTMEasting);
#ifdef __cplusplus
}
#endif
Function call in test_viewcontroller.m
double UTM_x;
double UTM_y;
UTMzone = [[NSString alloc] init];
UTMzone = (NSString *) LLtoUTM(latitude,longitude,UTM_y,UTM_x);
Can anyone tell me what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将文件更改为 test_viewcontroller.mm。
你告诉它编译为 Objective-C 文件,但它不理解引用。 '.mm' 表示 Objective-C++,它可以将 Obj-C 和 C++ 混合在一起,就像您尝试做的那样。
Change the file to be test_viewcontroller.mm.
You told it to compile as an Objective-C file, which doesn't understand references. '.mm' means Objective-C++, which can mix the Obj-C and C++ together like what you're attempting to do.
您根本无法在普通的 Objective-C 中执行此操作 - 因为 C 中不存在引用。它们是 C++ 功能。所以你必须使用 Objective-C++,这基本上意味着将你的 Objective-C 文件的扩展名更改为“.mm”。
You simply cannot do this in plain Objective-C — because references don't exist in C. They're a C++ feature. So you have to use Objective-C++, which basically means changing your Objective-C files' extensions to ".mm".