c++iosobjective-cxcode8

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.


Solution

  • 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