为什么在尝试编译此代码时会收到错误“error:unknown type name'virtual'”?

时间:2021-04-09 19:43:45

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]

[删除考虑我丢失了文件]

7 个解决方案

#1


22  

Please rename the main.m to main.mm. This worked for me.

请将main.m重命名为main.mm.这对我有用。

#2


2  

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”。这个对我有用。

#3


2  

Changing the name of file "AppDelegate.m" to "AppDelegate.mm". It's correct!

将文件“AppDelegate.m”的名称更改为“AppDelegate.mm”。这是正确的!

#4


2  

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!

我还必须为我在输入代码时引入的错误做一些其他修复 - 如果这不是唯一需要完成的事情,那么道歉,但它可能会帮助那些有类似问题的人!

#5


1  

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

#6


0  

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的唯一原因。

#7


0  

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.

头文件 不适合在C ++程序中使用。当我用C的stdio库 的c ++版本替换它时,你的代码就为我编译了。

#1


22  

Please rename the main.m to main.mm. This worked for me.

请将main.m重命名为main.mm.这对我有用。

#2


2  

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”。这个对我有用。

#3


2  

Changing the name of file "AppDelegate.m" to "AppDelegate.mm". It's correct!

将文件“AppDelegate.m”的名称更改为“AppDelegate.mm”。这是正确的!

#4


2  

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!

我还必须为我在输入代码时引入的错误做一些其他修复 - 如果这不是唯一需要完成的事情,那么道歉,但它可能会帮助那些有类似问题的人!

#5


1  

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

#6


0  

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的唯一原因。

#7


0  

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.

头文件 不适合在C ++程序中使用。当我用C的stdio库 的c ++版本替换它时,你的代码就为我编译了。