4

So I'm trying to use C++ inside my ios project.

After I make a new project (all default settings, fresh install of xcode), I create a Question.h and a Question.mm file, like this:

Question.h

#include <iostream>
#include <string>
using std::string;

class Question {
public:
   string text;
};


Question.mm

#include "Question.h"

it screams with errors like: Iostream: No such file or directory

using Xcode 3.2.6 with iOS SDK 4.3

What am I doing wrong?

marzapower
  • 5,531
  • 7
  • 38
  • 76
Andrei S
  • 6,486
  • 5
  • 37
  • 54

3 Answers3

5

The most likely cause of this is that you're including Question.h in a non-C++ file, for instance perhaps your AppDelegate is a .m file and you're trying to include Question.h there.

There are two solutions. First, you can make everything Objective-C++ (renaming all .m files to .mm). Historically I've found that to be extremely inconvenient because both Xcode and gdb have always had a lot of trouble with ObjC++ and you can get a lot of confusion (the dreaded "no this pointer" and "unknown language for stack frame" errors in gdb). I haven't done enough work with the latest versions of Xcode and gdb to determine if this is still a problem, but I suspect it is since gdb hasn't gotten a lot of work. Also, ObjC++ is slower to compile.

The other option is to wrap up your C++ into ObjC so that you can include it freely into pure ObjC portions of the code. This is the approach I usually take. ObjC++ is a bit of a mess of a language IMO, and I believe it's better to keep ObjC (.m) and C++ (.cpp) pure and separate with a thin layer of ObjC++ (.mm) to glue them together.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I still can't believe how blind I am. I WAS including Question.h in appDelegate. The funny thing is that I did a Project Search for Question.h and no result showed.. – Andrei S Jun 08 '11 at 13:08
1

Make sure your main.m file isn't importing your AppDelegate file. It may be using it like this:

NSStringFromClass([AppDelegate class])

but instead you can just do this:

@"AppDelegate"

That solved my problem.

0

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

Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • that doesn't fix it. besides, I have another project that i created some time ago, which uses h/mm files and they do compile successfully. can't point the difference between them tho.. – Andrei S Jun 08 '11 at 12:52
  • As a side note: if you *can* rename Question.mm to Question.cpp, you should. Pure C++ is easier to manage and faster to compile than Objective-C++. If you need access to just a couple of Cocoa-like things (like reading an NSArray), you can also use Core Foundation, which is a pure-C interface to many of the Cocoa data structures. You can include that in C++ without switching to Objective-C++. Obviously there are some places that ObjC++ is required, but when you don't need it, it's nice to avoid. – Rob Napier Jun 08 '11 at 13:23