user2125861
user2125861

Reputation:

xcode 4.6 iostream file not found error?

I am facing a problem regarding iostream file not found in header file.I just added a c++ file in my project a header file also included by default with some macro definition and including iostream file as

#ifndef __ObjectiveCPlus__File__
#define __ObjectiveCPlus__File__
#include <iostream>
#endif

but at this line I am getting error at include line as

enter image description here

I google it a lot and found various types of answer regarding this.But no one is able to correct my errors. Please help Thanks!

Upvotes: 1

Views: 10435

Answers (2)

Jesus CMD
Jesus CMD

Reputation: 184

I got the solution from another post:

Renaming your implementation file with .mm extension instead of .m will solve the issue.

Upvotes: 0

WDUK
WDUK

Reputation: 19030

You don't need <iostream> in your header file, put it in your .cpp file. You're not referring to anything in the iostream library in your header file, using this library is more of an implementation detail.

Why?

I believe UIAppDelegate imports UIViewController.h, that includes MathUtils.h. Because UIAppDelegate's implementation is in a .m file, it's being compiled for Objective-C, and this chain of includes (which is all based on the header files) is including something that is C++. As such, the Objective-C portion is unable to find <iostream>, as that library does not exist in pure Obj-C.

Putting it in your .cpp file limits it to one compilation unit, the MathUtils unit. Having it in your header file includes it in all compilation units that have a dependancy on whatever is using it, which may not be Objective C++.

Alternative Solution

You could have your whole project as Objective C++ (in this case, by changing UIAppDelegate.m to UIAppDelegate.mm), which means C++ can be used throughout. I'm not a fan of this method, and it could mask bad coding practices.

Upvotes: 3

Related Questions