xcode 为什么在尝试编译此代码时会收到错误“错误:未知类型名称‘虚拟’”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8177272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get the error "error: unknown type name 'virtual'" when trying to compile this code?
提问by DanZimm
Code:
代码:
struct IRenderingEngine {
virtual void Initialize(int width, int height) = 0;
virtual void Render() const = 0;
virtual void UpdateAnimation(float timeStep) = 0;
virtual void OnRotate(DeviceOrientation newOrientation) = 0;
virtual ~IRenderingEngine() {}
};
Learning opengles from a book for 3d iphone programming and it uses this example code but the book is targeted for xcode 3.x
从一本关于 3d iphone 编程的书中学习 opengles,它使用了这个示例代码,但这本书是针对 xcode 3.x 的
Somehow I feel like its something with xcode 4....
不知何故,我觉得 xcode 4 有点像......
EDIT:
编辑:
Heres the actual error:
这是实际的错误:
/Users/Dan/Documents/opengles/Hello Arrow/Hello Arrow/IRenderingEngine.hpp:27:2: error: unknown type name 'virtual' [1]
/Users/Dan/Documents/opengles/Hello Arrow/Hello Arrow/IRenderingEngine.hpp:27:2:错误:未知类型名称“虚拟”[1]
And that legitamtely is all that it takes to fail to compile, absolutely no other files. (Yes I've tried compiling with literally a main.m and this hpp file)
这就是编译失败所需要的一切,绝对没有其他文件。(是的,我试过用 main.m 和这个 hpp 文件编译)
It is recognizing the hpp file as a cpp header file though, if I try to add it to the compiled files it says that "no rule to process file '$(PROJECT_DIR)/Hello Arrow/IRenderingEngine.hpp' of type sourcecode.cpp.h for architecture i386" so I really have no idea what is going on
但是,它将 hpp 文件识别为 cpp 头文件,如果我尝试将其添加到编译文件中,它会说“没有规则来处理 sourcecode.cpp 类型的文件 '$(PROJECT_DIR)/Hello Arrow/IRenderingEngine.hpp' .h 架构 i386" 所以我真的不知道发生了什么
Note that I compiled with main.m meaning I compiled another Cocoa/Foundation based application
请注意,我用 main.m 编译意味着我编译了另一个基于 Cocoa/Foundation 的应用程序
I tried compiling for a c++ application and everything worked out just fine.... Similarly compiling with a main.mm test file worked fine too
我尝试编译一个 C++ 应用程序,一切都很好......同样用 main.mm 测试文件编译也工作正常
heres the actual project, lemme know how insane I really am:
这是实际项目,让我知道我真的很疯狂:
[Removed considering I lost the file]
[因文件丢失而删除]
回答by DancOfDeth
Please rename the main.m to main.mm. This worked for me.
请将 main.m 重命名为 main.mm。这对我有用。
回答by Steve Kim
If you're using Xcode 4, try changing the name of file "AppDelegate.m" to "AppDelegate.mm". It works for me.
如果您使用的是 Xcode 4,请尝试将文件“AppDelegate.m”的名称更改为“AppDelegate.mm”。这个对我有用。
回答by tig
Changing the name of file "AppDelegate.m" to "AppDelegate.mm". It's correct!
将文件“AppDelegate.m”的名称更改为“AppDelegate.mm”。这是正确的!
回答by Adrian Bigland
I moved the #import "IRenderingEngine.hpp" line from the GLView.h file to the GLView.mm - this prevented it from being imported into the main.m and HelloArrowAppDelegate.m files when they were compiled - and restricted the import into the .mm file, that could handle the C++.
我将#import "IRenderingEngine.hpp" 行从 GLView.h 文件移动到 GLView.mm - 这阻止了它在编译时被导入到 main.m 和 HelloArrowAppDelegate.m 文件中 - 并限制了导入到.mm 文件,可以处理 C++。
I also had to make a couple of other fixes for bugs I'd introduced when typing in the code - so apologies if that wasn't the only thing that needed to be done, but it might help those with similar problems!
我还必须对我在输入代码时引入的错误进行一些其他修复 - 如果这不是唯一需要做的事情,那么很抱歉,但它可能会帮助那些有类似问题的人!
回答by Sosily
if you call C++ files ( even if you only import them ) you need to change the .m file that call's it to .mm
如果您调用 C++ 文件(即使您只导入它们),则需要将调用它的 .m 文件更改为 .mm
回答by John Humphreys - w00te
This is just a stupid guess since I've never tried compiling something with the word virtual in a C compiler... but is there any chance that you were trying to compile this C++ code as C code? That's the only reason I can think of that a compiler wouldn't understand the keyword virtual.
这只是一个愚蠢的猜测,因为我从未尝试过在 C 编译器中使用 virtual 一词编译某些东西……但是您是否有可能尝试将此 C++ 代码编译为 C 代码?这是我能想到的编译器无法理解关键字 virtual 的唯一原因。
回答by Greg Satir
The header <stdlib.h> is not the right one to use in a C++ program. When I replaced it with the c++ version of C's stdio library <cstdlib> then your code compiled for me.
头文件 <stdlib.h> 不适合在 C++ 程序中使用。当我用 C 的 stdio 库 <cstdlib> 的 C++ 版本替换它时,你的代码为我编译。