c++ - 代码 : C++ error: unknown type name 'class' did you mean 'Class'

标签 c++ ios objective-c xcode8

我试图在我的 Xcode 项目中实现 C++ 类,但出现以下错误:

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

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

enter image description here 我正在添加头文件:

enter image description here 之后,我将其添加到头文件中:

#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 ()

你们中的任何人都知道为什么会出现此错误或我该如何解决或是否有解决此问题的方法?

非常感谢您的帮助。

最佳答案

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

关于c++ - 代码 : C++ error: unknown type name 'class' did you mean 'Class' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43478289/

相关文章:

c++ - 函数无法匹配 GLDEBUGPROC,仅在 MSVC 上(使用相同的 glew 版本 2.1.0,在 Linux 上使用 gcc/clang 没问题)

c++ - 编辑 EDIT 导致程序崩溃

ios - UITextField - 下一个按钮

objective-c - 音频缓冲区代码中的内存不断增长

c++ - 使用 Boost Coroutine(1.55) 的段错误不明确?

ios - 如何在 Swift 中以编程方式更改 Facebook 按钮的文本?

ios - SwiftUI:当数据通过工作表添加到 Realm 数据库时, View 不会更新

ios - 我的应用程序可以要求 iPhone 5S 吗?

ios - 如何在UIView中绘制贝塞尔曲线

c++ - 读取字符后跟数字c++