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 = dis开发者_如何学JAVAtance / 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!
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.
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.
精彩评论