STL Vector 的 Typedef 无法在 XCode 中编译
我有一些用 C++ 编写的代码,我想在我的 iPhone 应用程序中使用它们。我将源文件添加到 XCode 项目中,但我对源代码的某些部分有问题,例如我有以下代码:
#import <vector>
// (...) some other code
typedef std::vector<keypoint> keypointslist;
// (...) rest of the code
在我得到的 typedef 行中:
“:”标记之前应有“=”、“,”、“;”、“asm”或“属性”
在 Linux 机器上用 gcc 编译完全相同的代码,所以我想知道为什么 XCode有问题。
有什么建议吗?
I have some code written in C++ which I would like to use in my iPhone app. I added the source files to the XCode project, but I have problem with some parts of the source code, e.g. I have he following code:
#import <vector>
// (...) some other code
typedef std::vector<keypoint> keypointslist;
// (...) rest of the code
In the line with the typedef I'm getting:
Expected '=', ',', ';', 'asm' or 'attribute' before ':' token in
Exactly the same code was compiled with gcc on Linux machine, so I wonder why XCode has problem with it.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您确定,您告诉 XCode 将文件编译为 C++ 而不是 C 或 Objective-C 吗?也许您必须使用文件扩展名“.cpp”。
Are you sure, you told XCode to compile the file as C++ and not as C or Objective-C? Maybe you must use the file extension ".cpp".
它称为
#include
,而不是#import
:It's called
#include
, not#import
:+1 Ludger,
您要将其添加到 Objective C 应用程序中,您需要将 .cp/.mm/.cpp 翻译单元添加到项目中才能进行编译 - 右键单击项目类选择添加类确保您从 cpp 部分添加一个。
+1 with Ludger,
youre adding that to an objective c app, you'll need to add a .cp/.mm/.cpp translation unit to the project to get that to compile - right click on project classes select add class make sure you add one from the cpp section.
如果在“.m”文件中包含 C++ 代码,则必须将扩展名重命名为“.mm”或“.M”。即使您从目标 C 类引用 C++ 类,这一点也适用,必须重命名该文件。有时,这种重命名会产生多米诺骨牌效应,因此您的项目中最终会出现大量“.mm”文件。
If you include C++ code in a ".m" file you must rename the extension to ".mm" or ".M". this applies even if you reference a C++ class from an objective C one, the file must be renamed. Sometimes this renaming has a domino effect and so you end up with a lot of ".mm" files in your project.
确保在 typedef 之前包含
头文件。看起来编译器不知道std
命名空间。Make sure you included
<vector>
header file before typedef. It looks like the compiler doesn't knowstd
namespace.您必须重命名所有使用带有 .mm 扩展名的 C++ 代码的文件。
其中包括可以导入 C++ 类的视图控制器文件。例如。 MyViewController.mm
You have to rename the ALL files that use the C++ code with .mm extension.
That includes your view controller files that could be importing the C++ class. eg. MyViewController.mm