当在iPhone / iPad项目中使用#import C ++头文件时出现问题 [英] Problem when #import C++ Header File in iPhone/iPad Project

查看:219
本文介绍了当在iPhone / iPad项目中使用#import C ++头文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C ++类,我想在iPhone / iPad项目中使用

我以不同的方式创建了这个文件(像New File=> C ++),并且错误总是相同的。



当我编译项目时没有任何#import(.h C ++类) >

但是,一旦我#头导入头文件在我的一个头目标c文件,我得到错误如:



错误:vector:没有这样的文件或目录



< blockquote>



错误:expected'=' ',',';','asm'或'属性之前':'token'



我试图在文件信息中设置不同的文件类型(C ++类),重命名我的objc类在.mm等,但它似乎不工作。



所以我必须在objc头文件中缺少导入.h c ++类的东西,但是什么:p ^^



解决方案感谢Vlad

1°)要包含标题c ++文件:

  #ifdef __cplusplus 
#includeTriangulate.h
#endif

2°)将.mm中的objc文件重命名为他的文件信息(右键)设置文件类型为sourcecode.cpp.objcpp



感谢您的帮助! >
Vincent

解决方案


注意:Xcode要求文件名
编译器启用
的Objective-C ++扩展的.mm扩展。


试图使用C ++在Objective-C代码驻留在一个文件与 .m 扩展是最可能的原因的问题,因为编译器只是不能识别C ++构造根据错误信息。将 .m 文件重命名为 .mm 应该有帮助。



有关详情,请参阅

假设你想在Objective-C ++源文件中使用Objective-C类,没有问题。唯一的限制是您的 .h 文件必须是Objective-C清洁。这意味着你不能使用任何C ++ - isms,或者如果你必须将它们全部包装在#ifdef __cplusplus中。标题将在ObjC模式下编译,当它被纯粹的Objective-C文件包含时,所以它必须消除这种情况的任何C ++ isms (1)。所以你的Objective-C头文件应该包括这样的C ++头文件:

  #ifdef __cplusplus 
#include MyCPPHeader.h
#endif


I have a C++ class I would like to use in an iPhone/iPad project.
I created this file in different ways (like with "New File" => C++) and the error is always the same.

When I compile the project without having any #import (of the .h C++ class), it's ok.

But as soon as I #import the header file in one of my header objective-c file, I get error like :

error: vector: No such file or directory

or

error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"

I tried setting different values to the file type (of C++ class) in File Info, renaming my objc class in .mm etc, but it doesn't seem to work.

So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^

SOLUTION thanks to Vlad
1°) To include the header c++ file :

#ifdef __cplusplus
    #include "Triangulate.h"
#endif

2°) Renaming the objc file in .mm AND in his File Info (right clic) setting file type as sourcecode.cpp.objcpp

Thanks for helping !
Vincent

解决方案

Note: Xcode requires that file names have a ".mm" extension for the Objective-C++ extensions to be enabled by the compiler.

Trying to use C++ in Objective-C code residing in a file with .m extension is the most probable cause of the problem because compiler just does not recognize C++ constructs according to the error message. Renaming your .m file to .mm should help.

For more details, see Using C++ with Objective-C.

Assuming you want to use an Objective-C class in an Objective-C++ source file, there's no problem at all. The only restriction is that your .h file must be Objective-C clean. This means that you can't use any C++-isms in it, or that if you do you must wrap them all in #ifdef __cplusplus. The header will be compiled in ObjC mode when it's #included by a plain Objective-C file, so it has to eliminate any C++isms for that situation (1). So your Objective-C header file should include C++ header like this:

#ifdef __cplusplus
# include MyCPPHeader.h
#endif

这篇关于当在iPhone / iPad项目中使用#import C ++头文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆