STL Vector 的 Typedef 无法在 XCode 中编译

发布于 2024-10-01 02:42:51 字数 423 浏览 0 评论 0原文

我有一些用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

琉璃梦幻 2024-10-08 02:42:51

您确定,您告诉 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".

捎一片雪花 2024-10-08 02:42:51

它称为#include,而不是#import

#include <vector>

It's called #include, not #import:

#include <vector>
云柯 2024-10-08 02:42:51

+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.

梦途 2024-10-08 02:42:51

如果在“.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.

风铃鹿 2024-10-08 02:42:51

确保在 typedef 之前包含 头文件。看起来编译器不知道 std 命名空间。

Make sure you included <vector> header file before typedef. It looks like the compiler doesn't know std namespace.

っ〆星空下的拥抱 2024-10-08 02:42:51

您必须重命名所有使用带有 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文