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.
我想在Objective-C中使用/重用c++对象。我有你好。h有类定义,还有hello。cpp类的实现。
class Hello
{ int getX() ... };
And I use this class in Objective-C function.
我在Objective-C函数中使用这个类。
#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.
当我在Xcode中编译代码时,我得到了这个错误消息。
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?
到底是哪里出了错?
2 个解决方案
#1
34
Make sure you compile that file as "Objective-C++".
确保将该文件编译为“Objective-C++”。
The simplest way is to rename it as *.mm.
最简单的方法是将它重命名为*.mm。
If you don't want to rename the *.m file,
如果不想重命名*。m文件,
- Select your file.
- 选择您的文件。
- Open the File Info dialog (Cmd+I)
- 打开文件信息对话框(Cmd+I)
- In File Type, select "sourcecode.cpp.objcpp"
- 在文件类型中,选择“sourcecode.cpp.objcpp”
#2
7
Rename the Objective-C file from filename.m
to filename.mm
to make it compile as Objective-C++.
将Objective-C文件从文件名重命名。m文件名。将其编译为objective - c++。
#1
34
Make sure you compile that file as "Objective-C++".
确保将该文件编译为“Objective-C++”。
The simplest way is to rename it as *.mm.
最简单的方法是将它重命名为*.mm。
If you don't want to rename the *.m file,
如果不想重命名*。m文件,
- Select your file.
- 选择您的文件。
- Open the File Info dialog (Cmd+I)
- 打开文件信息对话框(Cmd+I)
- In File Type, select "sourcecode.cpp.objcpp"
- 在文件类型中,选择“sourcecode.cpp.objcpp”
#2
7
Rename the Objective-C file from filename.m
to filename.mm
to make it compile as Objective-C++.
将Objective-C文件从文件名重命名。m文件名。将其编译为objective - c++。