如何在 XCode 中使用 C++ 和 Objective-C [英] How can I use C++ with Objective-C in XCode
本文介绍了如何在 XCode 中使用 C++ 和 Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Objective-C 中使用/重用 C++ 对象.我有一个包含类定义的 hello.h 和用于类实现的 hello.cpp.
class 你好{ int getX() ... };
我在 Objective-C 函数中使用了这个类.
#include "hello.h"...- (IBAction) adderTwo:(id)sender{你好 *hi = new Hello();int value = hi->getX();NSLog(@"你好 %d", value);[textField setIntValue:value];
当我在 Xcode 中编译代码时,我收到此错误消息.
<前>班级 你好 *XXXXX用户/smcho/Desktop/cocoa/adderTwo/hello.h:9:0/Users/smcho/Desktop/cocoa/adderTwo/hello.h:9:错误:在 'Hello' 之前应为 '=', ',', ';', 'asm' 或 '__attribute__'出了什么问题?
解决方案
确保将该文件编译为Objective-C++".
最简单的方法是将其重命名为*.mm.
如果不想重命名*.m文件,
- 选择您的文件.
- 打开文件信息"对话框 (Cmd+I)
- 在文件类型中,选择sourcecode.cpp.objcpp"
I want to use/reuse C++ object with Objective-C. I have a hello.h that has the class definition, and hello.cpp for class implementation.
class Hello
{ int getX() ... };
And I use this class in Objective-C function.
#include "hello.h"
...
- (IBAction) adderTwo:(id)sender
{
Hello *hi = new Hello();
int value = hi->getX();
NSLog(@"Hello %d", value);
[textField setIntValue:value];
When I compile the code in Xcode, I get this error message.
class Hello *XXXXX Users/smcho/Desktop/cocoa/adderTwo/hello.h:9:0 /Users/smcho/Desktop/cocoa/adderTwo/hello.h:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Hello'
What went wrong?
解决方案
Make sure you compile that file as "Objective-C++".
The simplest way is to rename it as *.mm.
If you don't want to rename the *.m file,
- Select your file.
- Open the File Info dialog (Cmd+I)
- In File Type, select "sourcecode.cpp.objcpp"
这篇关于如何在 XCode 中使用 C++ 和 Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文