简体   繁体   English

IOS App中的C ++类,Xcode 5

[英]C++ Class in IOS App, Xcode 5

I have an IOS app that is a single view application. 我有一个IOS应用程序是一个单一的视图应用程序。 It has a standard C++ class that I made in it, however, it is not accepting the cpp class. 它有一个我在其中制作的标准C ++类,但是,它不接受cpp类。 Here is a simplified version of the .h file(without those lines #s): 这是.h文件的简化版本(没有那些行#s):

1 #ifndef __Calculator__Numbers__
2 #define __Calculator__Numbers__
3 #include <iostream>
4 class NumDigits
5 {
6 };
7 #endif 

I get the error: 'iostream' file not found 我收到错误: 'iostream' file not found

It appears as if the project doesn't have the C++ libraries? 看起来好像项目没有C ++库? If this is it, how would I add them? 如果是这样,我将如何添加它们? If not, what should I do to fix this error? 如果没有,我该怎么做才能解决这个错误? It looks like the cpp libraries are not included in my project: http://i.imgur.com/ZkJHb7j.png 看起来我的项目中没有包含cpp库: http//i.imgur.com/ZkJHb7j.png

Implementation files have the extension .m in Obj-C . 实现文件在Obj-C中具有扩展名.m To use a C++ file in your Xcode project with Objective-C you must use .mm extension and you can include C++ header in the .mm file. 要使用Objective-C在Xcode项目中使用C ++文件,必须使用.mm扩展名,并且可以在.mm文件中包含C ++标头。 You mustn't include the header in .m file, but if you want to include your C++ header in .h , you will need a macro like: 您不能在.m文件中包含标头,但如果要在.h包含C ++标头,则需要一个宏,如:

#ifdef _CP
#include <iostream>
#endif

To solve this, I just took out all the macros, #include s, and class declarations: 为了解决这个问题,我只是取出了所有的宏, #include和类声明:

#ifndef __Calculator__Numbers__
#define __Calculator__Numbers__
#include <iostream>
class NumDigits
{
};
#endif 

and left only the function declarations: void myFunction(int myVariable) ; 并且只留下函数声明: void myFunction(int myVariable) ; Then in the .cpp: 然后在.cpp中:

#include <iostream> //and other #includes
void myFunction(int myVariable) {
//stuff
}

This worked because the functions were still called and values were passed to and from them. 这是有效的,因为函数仍然被调用,值传递给它们和从它们传递。 The return values were the values that should've been spit out. 返回值是应该吐出的值。 when in the .mm if the .cpp was #included , the iostream file couldn't be found so 3include the .h ,and in the .cpp , #include the .h 如果在.mm如果.cpp#included ,则无法找到iostream文件,因此3include the .h ,而在.cpp#include .h

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

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