如何在XCode中将C ++与Objective-C结合使用 [英] How can I use C++ with Objective-C in XCode

查看:77
本文介绍了如何在XCode中将C ++与Objective-C结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Objective-C中使用/重用C ++对象. 我有一个具有类定义的hello.h和一个用于类实现的hello.cpp.

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() ... };

然后我在Objective-C函数中使用此类.

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];

当我在Xcode中编译代码时,收到此错误消息.

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'

出了什么问题?

推荐答案

确保将文件编译为"Objective-C ++".

Make sure you compile that file as "Objective-C++".

最简单的方法是将其重命名为 *.mm .

The simplest way is to rename it as *.mm.

如果您不想重命名* .m文件,

If you don't want to rename the *.m file,

  1. 选择文件.
  2. 打开文件信息"对话框(Cmd + I)
  3. 在文件类型"中,选择"sourcecode.cpp.objcpp"

这篇关于如何在XCode中将C ++与Objective-C结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆