Xcode:C ++错误:未知类型名称' class'你的意思是' Class'

时间:2017-04-18 17:09:23

标签: 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 ()

你们中的任何人都知道这个错误的原因,或者我该如何修复或者是否有解决方法?

我非常感谢你的帮助。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

将文件扩展名从.m(目标C)更改为.mm(目标C ++),您的编译将更加顺畅。