[英]Why do I get the error “error: unknown type name 'virtual'” when trying to compile this code?
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:错误:未知类型名称'virtual'[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头文件,但如果我尝试将其添加到编译文件中,则表示“没有规则处理文件'$(PROJECT_DIR)/ Hello Arrow / IRenderingEngine.hpp'类型为sourcecode.cpp .h for architecture 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] [删除考虑我丢失了文件]
Please rename the main.m to main.mm. 请将main.m重命名为main.mm. This worked for me. 这对我有用。
Changing the name of file "AppDelegate.m" to "AppDelegate.mm". 将文件“AppDelegate.m”的名称更改为“AppDelegate.mm”。 It's correct! 这是正确的!
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! 我还必须为我在输入代码时引入的错误做一些其他修复 - 如果这不是唯一需要完成的事情,那么道歉,但它可能会帮助那些有类似问题的人!
If you're using Xcode 4, try changing the name of file "AppDelegate.m" to "AppDelegate.mm". 如果您使用的是Xcode 4,请尝试将文件名“AppDelegate.m”更改为“AppDelegate.mm”。 It works for me. 这个对我有用。
如果你调用C ++文件(即使你只导入它们),你需要更改调用它的.m文件.mm
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? 这只是一个愚蠢的猜测,因为我从来没有尝试在C编译器中使用virtual这个词进行编译......但是你有没有机会尝试将这个C ++代码编译为C代码? That's the only reason I can think of that a compiler wouldn't understand the keyword virtual. 这是我能想到编译器无法理解关键字virtual的唯一原因。
The header <stdlib.h> is not the right one to use in a C++ program. 头文件<stdlib.h>不适合在C ++程序中使用。 When I replaced it with the c++ version of C's stdio library <cstdlib> then your code compiled for me. 当我用C的stdio库<cstdlib>的c ++版本替换它时,你的代码为我编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.