Why do I get the error “error: unknown type name 'virtual'” when trying to compile this code?

后端 未结 7 2463
庸人自扰
庸人自扰 2021-02-14 01:41

Code:

struct IRenderingEngine {
    virtual void Initialize(int width, int height) = 0;
    virtual void Render() const = 0;
    virtual void UpdateAnimation(flo         


        
相关标签:
7条回答
  • 2021-02-14 01:55

    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.

    0 讨论(0)
  • 2021-02-14 01:57

    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!

    0 讨论(0)
  • 2021-02-14 01:58

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

    0 讨论(0)
  • 2021-02-14 01:58

    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.

    0 讨论(0)
  • 2021-02-14 02:01

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

    0 讨论(0)
  • 2021-02-14 02:06

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

    0 讨论(0)
提交回复
热议问题