C++ 未知类型名称“类”;您指的是 'Class' 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8588734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unknown type name 'class'; did you mean 'Class'?
提问by Vanya
I'm trying to implement AQRecorder.h class from SpeakHere Apple Xcode project example, but even I rename my implementation class to ext. *.mm
and put line with #import "AQRecorder.h"
still getting error "Unknown type name 'class'; did you mean 'Class'?"
and many others.
Which according to me means that it is not recognized as C++ class.
我正在尝试从 SpeakHere Apple Xcode 项目示例中实现 AQRecorder.h 类,但即使我将实现类重命名为 ext。*.mm
并与#import "AQRecorder.h"
仍然出现错误"Unknown type name 'class'; did you mean 'Class'?"
和许多其他错误保持一致。根据我的说法,这意味着它不被识别为 C++ 类。
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by Diziet
I've just had this exact problem. I had a view controller using the AQRecorder class from AQRecorder.mm.
我刚刚遇到了这个确切的问题。我有一个使用 AQRecorder.mm 中的 AQRecorder 类的视图控制器。
When I included AQRecorder.h in my view controller these errors occurred. It appeared to me because my straight objective-c view controller (named as a .m file) was including C++ header files the compiler was throwing spurious errors.
当我在视图控制器中包含 AQRecorder.h 时,发生了这些错误。在我看来,因为我的直接 Objective-c 视图控制器(命名为 .m 文件)包含 C++ 头文件,编译器会抛出虚假错误。
There are two solutions. The quickest is to rename the view controller class including AQRecorder.h to be a .mm file, in my case UIRecorderViewController from .m to .mm.
有两种解决方案。最快的方法是将包含 AQRecorder.h 的视图控制器类重命名为 .mm 文件,在我的情况下,将 UIRecorderViewController 从 .m 重命名为 .mm。
Or, move the following includes:
或者,移动以下内容:
#include "CAStreamBasicDescription.h"
#include "CAXException.h"
Out of AQRecorder.h into AQRecorder.mm. This means that straight C++ style header files will no longer be included (by reference) in your plain Obj-C source.
出 AQRecorder.h 变成 AQRecorder.mm。这意味着直接的 C++ 风格的头文件将不再包含在你的普通 Obj-C 源代码中(通过引用)。
Hope that helps, and makes sense.
希望有帮助,并且有意义。
回答by Steve HHH
In my case, this error was caused by cyclical "Import" statements in two classes: the header file for each class included the header of the other class, resulting in the Unknown type name 'ClassA'; did you mean 'ClassB'?error:
在我的例子中,这个错误是由两个类中的循环“导入”语句引起的:每个类的头文件都包含另一个类的头文件,导致未知类型名称“ClassA”;您指的是“ClassB”吗?错误:
This is how my import statements were configured when I got this error. In ClassA.h
:
当我收到此错误时,这就是我的导入语句的配置方式。在ClassA.h
:
Import "ClassB.h"
In ClassB.h
:
在ClassB.h
:
Import "ClassA.h"
To fix it, I used the @class
forward declaration directive to forward-declare ClassA in ClassB.h
(this promises the pre-compiler that ClassA is a valid class, and that it will be available at compile time). For example:
为了解决这个问题,我使用了@class
前向声明指令来前向声明 ClassA ClassB.h
(这向预编译器保证 ClassA 是一个有效的类,并且它将在编译时可用)。例如:
In ClassA.h
:
在ClassA.h
:
Import "ClassB.h"
In ClassB.h
:
在ClassB.h
:
@class ClassA;
This fixed the Unknown type name 'ClassA'error, but also introduced a new error: ClassB.m
: Receiver type 'ClassA' for instance message is a forward declaration.For example:
这修复了Unknown type name 'ClassA'错误,但也引入了一个新错误:ClassB.m
: Receiver type 'ClassA' for instance message is a forward declaration。例如:
To fix this new error, I had to import ClassA.h
at the top of the implementation file of ClassB (ClassB.m
). Both errors are now resolved, and I get zero errors and warnings.
为了修复这个新错误,我不得不ClassA.h
在 ClassB ( ClassB.m
)的实现文件的顶部导入。现在这两个错误都已解决,我得到零错误和警告。
For example, I now have:
例如,我现在有:
In ClassA.h
:
在ClassA.h
:
Import "ClassB.h"
In ClassB.h
:
在ClassB.h
:
@class ClassA;
In ClassB.m
:
在ClassB.m
:
Import "ClassA.h"
Both error messages are now resolved.
这两个错误消息现在都已解决。
回答by Zhou
i met the same error with you, hope my solution may help you. The Xcode compiler could compile objective-c & c++ in the "*.mm" file, so you may change all your filename which import "AQRecorder.h"(all direct & indirect) file with ".mm" postfix. But you may not do that, you may find that the relationship between SpeakHereController and SpeakHereViewController is some tricky, i just learned how he used it, that create the SpeakHereController object in a nib file, so SpeakHereViewController file is not have to import the "AQRecorder.h" file. my English is stupid, i hope my answer may help you.
我和你遇到了同样的错误,希望我的解决方案可以帮助你。Xcode 编译器可以在“*.mm”文件中编译 Objective-c 和 c++,因此您可以更改所有导入“AQRecorder.h”(所有直接和间接)文件的文件名,后缀为“.mm”。但是你可能不会这样做,你可能会发现 SpeakHereController 和 SpeakHereViewController 之间的关系有些棘手,我刚刚了解了他是如何使用它的,在一个 nib 文件中创建 SpeakHereController 对象,所以 SpeakHereViewController 文件不必导入“AQRecorder .h”文件。我的英语很笨,希望我的回答能帮到你。
回答by carmin
IMPORTANT: Select "Compile Source As" variable in compiler settings and set its value to "Objective-C++".
重要提示:在编译器设置中选择“Compile Source As”变量并将其值设置为“Objective-C++”。
回答by chicha
It looks like this problem is impossible to resolve. If it is possible to shift #include "myC++.h" into *.mm file then it works. But if You need to use it from your objectiveC.h file it fails. I guess this is bug from apple. There is a way to specify *.mm instead of *.m but there is nothing similar to *.hh instead of *.h
看起来这个问题是无法解决的。如果可以将 #include "myC++.h" 转换为 *.mm 文件,则它可以工作。但是如果你需要从你的objectiveC.h 文件中使用它,它就会失败。我猜这是来自苹果的错误。有一种方法可以指定 *.mm 而不是 *.m 但没有类似于 *.hh 而不是 *.h
回答by uspython
I fixed this problem today.If you #include or #import a C++ *.h file or C++/OC mixed *.h file in YourHeader.h,you MUST have a YourHeader.mm. If not,then all your C++ file and C++/OC mixed file will show compile errors.
today.If你#包括我解决了这个问题,或者#进口在C ++的* .h文件或C ++ / OC混合* .h文件中YourHeader.h,你必须有一个YourHeader.mm。如果没有,那么你所有的 C++ 文件和 C++/OC 混合文件都会显示编译错误。
回答by Totoro
This problem can be solved by changing the following build settings:
可以通过更改以下构建设置来解决此问题:
Under Apple LLVM Compiler 4.2 - Language
在 Apple LLVM Compiler 4.2 下 - 语言
C++ Language Dialect (Gnu++11 works for me) C++ Standard Library (Libc++ works for me)
C++ 语言方言(Gnu++11 对我有用) C++ 标准库(Libc++ 对我有用)
It is also necessary to name the .m files as .mm, if you are letting Xcode use file extension to decide how to compile each file.
如果您让 Xcode 使用文件扩展名来决定如何编译每个文件,则还需要将 .m 文件命名为 .mm。
回答by Fallenreaper
I resolved this issue the following:
我解决了以下问题:
Originally, the files were placed inside the project, but not inside the the correct file structure.
最初,文件被放置在项目中,但不在正确的文件结构中。
YES, I am aware it is not an actual structre as it is only for visual issues, BUT when i moved the Header and the CPP file inside the PROJ folder all worked.
是的,我知道它不是一个实际的结构体,因为它仅用于视觉问题,但是当我移动 PROJ 文件夹中的标题和 CPP 文件时,一切正常。
回答by Lindydancer
Using XCode, it's possible to use the "language" Objective C++, which allows you to mix Objective C and C++.
使用 XCode,可以使用“语言”Objective C++,它允许您混合使用 Objective C 和 C++。
回答by ubaltaci
My solution maybe looks ridiculus, but in xcode 4.2,
我的解决方案可能看起来很荒谬,但在 xcode 4.2 中,
To add Apple Audio examples codes, I moved all files individually, and it is worked as a charm!
为了添加 Apple Audio 示例代码,我单独移动了所有文件,这很有用!
I mean not dragging whole folder, drag one by one all individual file in a spesific folder.
我的意思是不要拖动整个文件夹,而是一个一个地拖动一个特定文件夹中的所有单个文件。