user2924482
user2924482

Reputation: 9120

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.

Upvotes: 3

Views: 5907

Answers (3)

RowanPD
RowanPD

Reputation: 393

I had a mix of C and C++ files so I had to wrap the C++ header contents with "#ifdef __cplusplus" as per below. This was immediately inside the usual "#ifndef" for the header.

#ifdef __cplusplus

#include <whatever.h>

class Blah { ... }

#endif /* __cplusplus */

Upvotes: 0

Stanislav Ageev
Stanislav Ageev

Reputation: 778

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

Upvotes: 4

Michael Dautermann
Michael Dautermann

Reputation: 89509

Change the file extension from .m (Objective C) to .mm (Objective C++) and your compile will go much smoother.

Upvotes: 1

Related Questions