error C2504: base class undefined

后端 未结 3 1485
陌清茗
陌清茗 2021-01-17 16:16

I have encountered this error many times before and eventually found solutions, but this one has me stumped. I have a class \'Mob\' that is inherited by class \'Player\'.

相关标签:
3条回答
  • 2021-01-17 16:30

    Forward declaring doesn't help for class Player : public Mob because the compiler needs the full definition for inheritance.

    So most likely one of your includes in Mob.h is bringing in Player.h which then puts Player ahead of Mob and thus triggers the error.

    0 讨论(0)
  • 2021-01-17 16:40

    I know this is not the best way to deal with that problem, but it work for me at least. you can put all your other includes in the cpp file:

    #include "OmiGame/OmiGame.h"
    #include "PlayState.h"
    #include "Mob.h"
    #include "resources.h"
    
    0 讨论(0)
  • 2021-01-17 16:43

    I have got through the similar problem and I out solution and made it a thumb rule for me

    Solution / Thumb Rule

    //File - Foo.h
    #include "Child.h"
    class Foo 
    {
    //Do nothing 
    };
    
    //File - Parent.h
    #include "Child.h" // wrong
    #include "Foo.h"   // wrong because this indirectly 
                       //contain "Child.h" (That is what is your condition)
    class Parent 
    {
    //Do nothing 
    Child ChildObj ;   //one piece of advice try avoiding the object of child in parent 
                       //and if you want to do then there are diff way to achieve it   
    };
    
    //File - Child.h
    #include "Parent.h"
    class Child::public Parent 
    {
    //Do nothing 
    };
    

    Don't include the child in parent class .

    if you want to know the way to have a child object in a parent class the refer the link Alternative

    Thank you

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