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

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

我正在尝试在我的 Xcode 项目中实施 C++ class,但出现以下错误:

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

这就是我正在做的。我正在添加新的 C++ 文件:

我正在添加头文件:

之后我将其添加到头文件中:

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

但是在我将 DoingSomething.hpp 添加到我的 viewController:

后错误开始显示
#import "ViewController.h"
#import "DoingSomething.hpp"

@interface ViewController ()

你们中有人知道为什么会出现这个错误,我该如何解决,或者是否有解决方法?

非常感谢你的帮助。

将文件扩展名从 .m (Objective C) 更改为 .mm (Objective C++),您的编译会更顺利。

问题是您在 Objective-C 文件(C 的超集)中使用 C++ header,这就是它无法识别 C++ 语法的原因。要使其正确编译,您应该只在 c++ 或 Objective-C++ (.mm) 源文件中包含 C++ headers。尝试将 viewControllers 文件扩展名从 .m 更改为 .mm