简体   繁体   English

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

[英]How can I use my custom types (C++) in an objective-c project with xcode?

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. 我做了很多工作,所以我想在我的ObjC iPhone应用程序中使用C ++类。 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. 我尝试将扩展名为.h和.mm的文件添加到我的xcode项目中,但似乎并未将它们视为C ++文件。 (Won't find any of the STL headers, syntax errors anywhere I declare a class or anything). (在我声明一个类的任何地方都找不到任何STL标头,语法错误)。

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 新信息:尝试使用.mm和.h扩展名,错误:在.h文件中: namespace DA以及涉及c ++代码的几乎所有其他内容(此消息共约40个错误)给了我expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

I also get *: No such file or directory for all instances of #include <*> throughout the c++ files. 我还得到*: No such file or directory在整个c ++文件中, #include <*>所有实例都*: No such file or directory

I'm sure its some kind of simple fix but .mm and .h wasn't enough. 我敢肯定它只是某种简单的解决方法,但是.mm和.h还不够。

Update: Found that copying and pasting source code into new files seems to work. 更新:发现将源代码复制并粘贴到新文件中似乎可行。 The mm file (or the cpp file) compiles. mm文件(或cpp文件)将编译。 The h file doesn't work though, it throws the above mentioned error at any occurrence of c++ specific code. 虽然h文件不起作用,但是在出现任何c ++特定代码时都会抛出上述错误。

Thanks! 谢谢!

You are probably including (or importing) your h file in one of your .m files. 您可能在一个.m文件中包含(或导入)了h文件。 Those need to be renamed to .mm too. 那些也需要重命名为.mm。

In the end you'll probably name all .m fikes to .mm. 最后,您可能会将所有.m分支命名为.mm。 And by the way, the cpp files, can simply remain cpp. 顺便说一下,cpp文件可以简单地保留为cpp。 No reason to rename them. 没有理由重命名它们。

我不知道到底要得到什么错误,而不是将扩展名为.hpp和.cpp的文件添加到项目中,而是将头文件的扩展名更改为.h,并将扩展名更改为.cpp文件转换为.mm或.M,以便Xcode将其识别为Objective-C ++文件,并进行适当的编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM