Unknown type name 'class'; did you mean 'Class'?

C++Objective CIos5

C++ Problem Overview


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.

Any help will be appreciated.

C++ Solutions


Solution 1 - C++

I've just had this exact problem. I had a view controller using the AQRecorder class from AQRecorder.mm.

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.

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.

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.

Hope that helps, and makes sense.

Solution 2 - C++

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:

enter image description here

This is how my import statements were configured when I got this error. In ClassA.h:

Import "ClassB.h"

In 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:

In ClassA.h:

Import "ClassB.h"

In 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:

enter image description here

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.

For example, I now have:

In ClassA.h:

Import "ClassB.h"

In ClassB.h:

@class ClassA;

In ClassB.m:

Import "ClassA.h"

Both error messages are now resolved.

Solution 3 - C++

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.

Solution 4 - C++

IMPORTANT: Select "Compile Source As" variable in compiler settings and set its value to "Objective-C++".

Solution 5 - C++

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

Solution 6 - C++

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.

Solution 7 - C++

Using XCode, it's possible to use the "language" Objective C++, which allows you to mix Objective C and C++.

Solution 8 - C++

My solution maybe looks ridiculus, but in xcode 4.2,

To add Apple Audio examples codes, I moved all files individually, and it is worked as a charm!

I mean not dragging whole folder, drag one by one all individual file in a spesific folder.

Solution 9 - C++

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.

Solution 10 - C++

This problem can be solved by changing the following build settings:

Under Apple LLVM Compiler 4.2 - Language

C++ Language Dialect (Gnu++11 works for me) C++ Standard Library (Libc++ works for me)

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.

Solution 11 - C++

From my own experience, following things should be taken care.

  1. Select "Compile Source As" variable in compiler settings and set its value to "Objective-C++" i.e Build Settings->Apple LLVM 5.1 - Language->Compile Source As->Objective-C++ (in Xcode 5.1.1)

  2. Change the relevant files which include a C++ header file from .m to .mm (sometimes you need to change all .m to .mm). This was answered by @Diziet above.

  3. If you face incompatible type errors, explicitly do type casting of the required type. These errors might not have shown up when it was .m file.

Solution 12 - C++

Specifically, if you're compiling a NDK C/C++ code, and got:

>Unknown type name 'jclass'; did you mean 'class'?

It is very likely that the file containing 'jclass' is #include-ed in .c/.cpp files (directly or indirectly) which are to be built into a library. And the solution is: remove that #include.

Solution 13 - C++

This is one of the common mistakes done : Circular dependencies.

Consider an example :

File : B.h

#import "A.h"

File : A.h

#import "C.h"

File : C.h

#import "B.h"

This introduces Circular dependency.

Just redeclare in C.h as :

@class B;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVanyaView Question on Stackoverflow
Solution 1 - C++DizietView Answer on Stackoverflow
Solution 2 - C++Steve HHHView Answer on Stackoverflow
Solution 3 - C++ZhouView Answer on Stackoverflow
Solution 4 - C++carminView Answer on Stackoverflow
Solution 5 - C++chichaView Answer on Stackoverflow
Solution 6 - C++uspythonView Answer on Stackoverflow
Solution 7 - C++LindydancerView Answer on Stackoverflow
Solution 8 - C++ubaltaciView Answer on Stackoverflow
Solution 9 - C++FallenreaperView Answer on Stackoverflow
Solution 10 - C++TotoroView Answer on Stackoverflow
Solution 11 - C++Rajaraman SubramanianView Answer on Stackoverflow
Solution 12 - C++ChrisZZView Answer on Stackoverflow
Solution 13 - C++Abhilash GopalView Answer on Stackoverflow