2017-04-18 109 views
0

我想实现我的Xcode项目中C++类,但我发现了以下错误: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() 

任何人都知道为什么这个错误,或者我该如何解决或者如果有解决方法?

我真的很感谢你的帮助。

+0

您的视图控制器是一个Objective-C++文件吗? – dan

+0

@dan,我的viewController是Objective-C文件 – user2924482

回答

1

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

1

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