error C2504: base class undefined

55,315

Solution 1

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.

Solution 2

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

Solution 3

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"
Share:
55,315
Greg Treleaven
Author by

Greg Treleaven

Updated on July 05, 2022

Comments

  • Greg Treleaven
    Greg Treleaven almost 2 years

    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'. This is Mob.h:

    #pragma once
    #include "PlayState.h"
    #include "OmiGame/OmiGame.h"
    #include "resources.h"
    
    class PlayState;
    
    class Mob
    {
    private:
        int frames;
        int width;
        int height;
        int time;
    
        sf::Texture textureL;
        sf::Texture textureR;
        Animation animationL;
        Animation animationR;
        AnimatedSprite sprite;
        bool moveLeft;
        bool moveRight;
        bool facingRight;
    
    public:
        void createMob(std::string l, std::string r, int frames, int width, int height, int time, int x, int y);
    
        void updateMob(omi::Game *game, PlayState *state);
        void drawMob(sf::RenderTarget &target);
    
        void setLeft(bool b) { moveLeft = b; }
        void setRight(bool b) { moveRight = b; }
        bool isLeft() { return moveLeft; }
        bool isRight() { return moveRight; }
    
        sf::Vector2f getPosition() { return sprite.getPosition(); }
    };
    

    This is Player.h, as of now it is extremely simple:

    #pragma once
    #include "OmiGame/OmiGame.h"
    #include "PlayState.h"
    #include "Mob.h"
    #include "resources.h"
    
    class PlayState;
    class Mob;
    
    const int playerFrames = 8;
    const int playerWidth = 16;
    const int playerHeight = 48;
    const int playerTime = 50;
    const int playerX = 200;
    const int playerY = 200;
    
    class Player : public Mob
    { //the error occurs at this line//
    public:
        Player();
        void update(omi::Game *game, PlayState *state);
        void draw(sf::RenderTarget &target);
    };
    

    And, as you can probably guess, this is the error:

    error C2504: 'Mob' : base class undefined   player.h
    

    I have forward declared mob, I have hopefully fixed any circular dependencies. Please can someone help me?

  • Greg Treleaven
    Greg Treleaven almost 10 years
    Removing #include "PlayState.h" worked. PlayState declares Player, but Player also declares PlayState so the line wasn't needed. Thanks.