Using C ++ in xcode / object c

So, I am trying to use C ++ inside my ios project.

After creating a new project (all default settings, new xcode installation), I create a file Question.h and Question.mm , for example:

 Question.h #include <iostream> #include <string> using std::string; class Question { public: string text; }; Question.mm #include "Question.h" 

he screams with errors: Iostream: No such file or directory

using Xcode 3.2.6 with iOS SDK 4.3

What am I doing wrong?

+4
source share
3 answers

The most likely reason for this is that you include Question.h in a non-C ++ file, for example, maybe your AppDelegate is a .m file, and you are trying to include Question.h there.

There are two solutions. First, you can do all Objective-C ++ (renaming all .m files to .mm). Historically, I found this to be extremely inconvenient because Xcode and gdb always had big problems with ObjC ++, and you can get a lot of confusion (the terrible errors “no this pointer” and “unknown language for the stack” in GDB). I have not done enough work with the latest versions of Xcode and gdb to determine if this is all a problem, but I suspect that since gdb did not get much work. In addition, ObjC ++ is slower to compile.

Another option is to end your C ++ in ObjC so that you can freely add it to the clean parts of ObjC code. This is the approach that I usually take. ObjC ++ is a bit of a mess of the IMO language, and I think it's best to keep ObjC (.m) and C ++ (.cpp) clean and split with a thin layer of ObjC ++ (.mm) to glue them together.

+5
source

Make sure your main.m file does not import the AppDelegate file. He can use it as follows:

NSStringFromClass([AppDelegate class])

but instead you can just do this:

@"AppDelegate"

This solved my problem.

+1
source

Try renaming Question.mm to Question.cpp as xcode decides which compiler to use based on the extension.

0
source

All Articles