Vanya
Vanya

Reputation: 5005

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

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.

Upvotes: 50

Views: 53136

Answers (14)

ChanOnly123
ChanOnly123

Reputation: 1024

Use only objective-c++ like this

enter image description here

Upvotes: 0

ChrisZZ
ChrisZZ

Reputation: 2151

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.

Upvotes: 0

Abhilash Gopal
Abhilash Gopal

Reputation: 419

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;

Upvotes: -1

Rajaraman Subramanian
Rajaraman Subramanian

Reputation: 801

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.

Upvotes: 0

carmin
carmin

Reputation: 419

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

Upvotes: 3

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.

Upvotes: 0

uspython
uspython

Reputation: 563

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.

Upvotes: 1

Fallenreaper
Fallenreaper

Reputation: 10684

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.

Upvotes: 0

chicha
chicha

Reputation: 21

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

Upvotes: 2

Diziet
Diziet

Reputation: 2417

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.

Upvotes: 50

Steve HHH
Steve HHH

Reputation: 13147

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.

Upvotes: 21

Zhou
Zhou

Reputation: 243

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.

Upvotes: 8

ubaltaci
ubaltaci

Reputation: 1016

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.

Upvotes: 0

Lindydancer
Lindydancer

Reputation: 26094

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

Upvotes: 0

Related Questions