2014-07-13 19 views
5

Napotkałem ten błąd wiele razy wcześniej i ostatecznie znalazłem rozwiązania, ale ten ma mnie zakłopotany. Mam klasę "Mob" odziedziczoną po klasie "Player". To Mob.h:error C2504: base class undefined

#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(); } 
}; 

to Player.h, jak na razie jest bardzo prosta:

#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); 
}; 

I, jak można się domyślić, jest to błąd:

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

Mam zadeklarowany mob, mam nadzieję, że naprawiłem wszelkie zależności kołowe. Czy ktoś może mi pomóc?

+0

Czy pliki znajdują się w tym samym katalogu? – ChiefTwoPencils

+0

Wiesz, to jakoś pokonuje enkapsulację, jeśli zapewnisz dostęp do wszystkich swoich prywatnych członków ... – Deduplicator

+1

@Deduplicator Nie ma. Tylko czterech członków prywatnych ma akcesorów, ponieważ moby muszą reagować inaczej na ich podstawie, ale muszą dzielić tych członków, aby ponownie wykorzystany kod mógł również uzyskać do nich dostęp. –

Odpowiedz

17

Deklaracja przekazywania dalej nie pomaga dla class Player : public Mob, ponieważ kompilator potrzebuje pełnej definicji dziedziczenia.

Najprawdopodobniej jeden z twoich graczy w grze Mob.h przynosi Player.h, który stawia Gracza przed Mobem i tym samym uruchamia błąd.

+0

Usunięcie '#include" PlayState.h "' działało. PlayState deklaruje gracza, ale Gracz również deklaruje PlayState, więc linia nie była potrzebna. Dzięki. –

6

Mam przez podobny problem, a ja się rozwiązanie i sprawiał, że reguła kciuka mi

Rozwiązanie/Thumb art

//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 
}; 

Nie podawaj dziecku w klasie nadrzędnej.

jeśli chcesz znać drogę mieć obiektu podrzędnego w klasie macierzystego patrz odnośnik Alternative

Dziękuję

1

wiem, że to nie jest najlepszy sposób na radzenie sobie z tym problemem, ale przynajmniej dla mnie działa. możesz umieścić wszystkie pozostałe elementy w pliku cpp:

#include "OmiGame/OmiGame.h" 
#include "PlayState.h" 
#include "Mob.h" 
#include "resources.h"