Why do I get the error “error: unknown type name &

2020-06-11 07:44发布

问题:

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

Somehow I feel like its something with 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]

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)

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

Note that I compiled with main.m meaning I compiled another Cocoa/Foundation based application

I tried compiling for a c++ application and everything worked out just fine.... Similarly compiling with a main.mm test file worked fine too

heres the actual project, lemme know how insane I really am:

[Removed considering I lost the file]

回答1:

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



回答2:

If you're using Xcode 4, try changing the name of file "AppDelegate.m" to "AppDelegate.mm". It works for me.



回答3:

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



回答4:

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++.

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:

if you call C++ files ( even if you only import them ) you need to change the .m file that call's it to .mm



回答6:

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.



回答7:

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.