Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: C++ error: unknown type name 'class' did you mean 'Class'

I'm trying to implement C++ class inside my Xcode project but I'm getting the following error:

Expected ';' after top level declarator
Unknown type name 'class'; did you mean 'Class'

enter image description here This is what I'm doing. I'm adding new C++ file:

enter image description here And I'm adding the header file:

enter image description here After that I'm adding this to the header file:

#ifndef DoingSomething_hpp
#define DoingSomething_hpp
#include <stdio.h>
class DoingSomething {
public:
    static DoingSomething *instance();
    int doSomething();
};
#endif /* DoingSomething_hpp */

But the error start showing after I add the DoingSomething.hpp to my viewController:

#import "ViewController.h"
#import "DoingSomething.hpp"

@interface ViewController ()

Any of you knows why of this error or how can I fixed or if there is work around this?

I'll really appreciate your help.

like image 579
user2924482 Avatar asked May 09 '26 08:05

user2924482


1 Answers

The problem is that you use C++ header in an Objective-C file (which is superset of C), that's why it doesn't recognize C++ syntax. To make it compile properly you should only include C++ headers in c++ or Objective-C++ (.mm) source files. Try changing the viewControllers file extension from .m to .mm

like image 168
Stanislav Ageev Avatar answered May 11 '26 23:05

Stanislav Ageev