50

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.

Vanya
  • 4,973
  • 5
  • 32
  • 57

13 Answers13

49

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.

Diziet
  • 2,397
  • 1
  • 26
  • 34
  • 2
    This. Also, if you're like me and need those C++ header files to declare ivars in your Objective-C class, you can use a class extension, as detailed here: http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++ – Siobhán Nov 08 '12 at 20:41
  • @SeanD. That's pretty nifty. – Diziet Nov 09 '12 at 09:02
  • 2
    You can also just change the file type in the "File Inspector" to "Objective-C++ Source". This has the same effect as changing the filename extension to .mm – Michael Wildermuth Apr 24 '15 at 18:30
21

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.

Steve HHH
  • 12,947
  • 6
  • 68
  • 71
  • 3
    The OP's error was literally `Unknown type name 'class'; did you mean 'Class'?`. In other words, it sounds like it's interpreting his C++ header as an Obj-C header instead of an Obj-C++ header. – Lily Ballard Mar 03 '12 at 22:23
  • Ah, I see. Thanks for that. The error I got was actually `Unknown type name 'JKJobVC'; did you mean 'JKJobDC'?` (where JKJobVC is a view controller and JKJobDC is a data controller). My answer contains the steps I performed to resolve that issue. I have edited the answer to include screenshots and improve the overall clarity. If the OP's error message was literal, my answer may be way off-base, but I do feel that it could help others who encounter `Unknown type name '*'; did you mean '*'?` errors of the sort that I encountered. – Steve HHH Mar 04 '12 at 07:49
  • I had the same error as the question, and this answer resolved my issue. – hatunike Sep 12 '13 at 03:19
  • This is a different error message and a different problem. – Steve Waddicor Sep 15 '19 at 13:04
8

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.

Zhou
  • 233
  • 1
  • 3
  • 12
3

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

carmin
  • 421
  • 3
  • 5
2

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

chicha
  • 21
  • 2
  • 8
    I guess I have found decision. In project options, in group of settings "Apple LLVM compiler 3.1 - Language" change option "Compile Sources As" to "Objective-C++". I guess this make compiler to assume C++ always regardless to file extension. – chicha Apr 29 '12 at 15:56
  • @peetonn No idea why SpeakHere works but all the sources in a new project don't. I've gone through all the Build Settings and made them the same with no joy :-( Setting Compile Sources As Objective-C++ *did* work though. – Ben Clayton Aug 09 '12 at 13:48
  • Setting `Compile Sources As` to `Objective-C++` solved all my woes. I think this is just an Xcode 4.1 -> 4.2 blunder in general... http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++ – jocull May 19 '13 at 21:37
1

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.

uspython
  • 483
  • 5
  • 18
  • 1
    That is not correct, the problem is because you mix Objective-C and C++ and some reserved words used in C++ are not recognised in ObjC. Below Sean-D gives some godd advices and valid solutions. – pocjoc Aug 15 '13 at 14:23
0

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.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
0

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.

Totoro
  • 3,398
  • 1
  • 24
  • 39
0

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.

0

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.

ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
0

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

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • I've tried this, but getting errors on the other places. The building settings I use are the same as the example use. – Vanya Dec 21 '11 at 11:23
0

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.

ubaltaci
  • 1,016
  • 1
  • 16
  • 27
-1

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;
Abhilash Gopal
  • 429
  • 5
  • 8