如何使用 xcode 在 Objective-C 项目中使用自定义类型 (C++)?

发布于 2024-11-07 11:04:39 字数 774 浏览 0 评论 0原文

我编写了一些类来透明地处理具有单位的数字,就好像它们只是整数或浮点数一样。简而言之:

SI_Term speed(4, "mph");
SI_Term distance(10000, "feet");
SI_Term time = distance / speed;
std::cout<<time.string();

所有主要运营商都超负荷地以这种方式工作。花了相当多的工作,所以我想在我的 ObjC iPhone 应用程序中使用 C++ 类。我尝试将它们与 .h 和 .mm 扩展名添加到我的 xcode 项目中,但它似乎没有将它们视为 C++ 文件。 (在我声明类或任何内容的任何地方都找不到任何 STL 标头、语法错误)。

新信息: 尝试了 .mm 和 .h 扩展名,错误: 在 .h 文件中: 命名空间 DA 以及涉及 C++ 代码的几乎所有其他内容(此消息中大约有 40 个错误)给了我预期的 '='、','、';'、'asm' 或 '__attribute__'在 ':' 标记 之前,

我还得到了整个 c++ 文件中 #include <*> 的所有实例的 *: No such file or directory

我确信这是某种简单的修复,但 .mm 和 .h 还不够。

更新: 发现将源代码复制并粘贴到新文件中似乎可行。 mm 文件(或 cpp 文件)编译。 h 文件不起作用,它会在任何出现 c++ 特定代码时引发上述错误。

谢谢!

I have a few classes I wrote to deal with numbers having units transparently as if they were just ints or floats. In a nutshell:

SI_Term speed(4, "mph");
SI_Term distance(10000, "feet");
SI_Term time = distance / speed;
std::cout<<time.string();

and all the major operators are overloaded to work this way. Took a quite a bit of work so I would like to use the C++ classes in my ObjC iPhone app. I tried adding them with .h and .mm extensions to my xcode project, but it doesn't seem to be treating them as C++ files. (Won't find any of the STL headers, syntax errors anywhere I declare a class or anything).

New Info:
Tried .mm and .h extensions, errors:
in a .h file:
namespace DA and just about everything else involving c++ code (about 40 errors all this message) gives me expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

I also get *: No such file or directory for all instances of #include <*> throughout the c++ files.

I'm sure its some kind of simple fix but .mm and .h wasn't enough.

Update:
Found that copying and pasting source code into new files seems to work. The mm file (or the cpp file) compiles. The h file doesn't work though, it throws the above mentioned error at any occurrence of c++ specific code.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挖鼻大婶 2024-11-14 11:04:39

您可能将 h 文件包含(或导入)到 .m 文件之一中。这些也需要重命名为 .mm。

最后,您可能会将所有 .m fike 命名为 .mm。顺便说一下,cpp 文件可以简单地保留为 cpp。没有理由重命名它们。

You are probably including (or importing) your h file in one of your .m files. Those need to be renamed to .mm too.

In the end you'll probably name all .m fikes to .mm. And by the way, the cpp files, can simply remain cpp. No reason to rename them.

葵雨 2024-11-14 11:04:39

不确切知道您遇到的错误是什么,而不是将带有 .hpp 和 .cpp 扩展名的文件添加到您的项目中,我会将头文件的扩展名更改为 .h,并将扩展名更改为 .cpp文件转换为 .mm 或 .M,以便 Xcode 将它们识别为 Objective-C++ 文件,并适当地编译它们。

Not knowing exactly what the errors you're getting are, rather than adding your files with a .hpp and .cpp extension to your project, I would change the extension of the header files to just .h, and the extension to your .cpp files to either .mm or .M so that Xcode recognizes them as Objective-C++ files, and compiles them appropriately.

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