1

Okay... I have understood this question about circular dependency and forward declaration, but I am having trouble understanding a particular error that involve inheritance and pointer-based variables.

I will show snippets of the relevant code:

Player is the derived class from Entity

Entity.hpp

class Entity : public sf::Sprite{

private:

    int health;
    float speed;
    sf::Time spawntime;
    bool invincible;


protected:

    SceneGame *myScene; // let's keep it at null
                        // since setScene will take care of it

   public:

// more code....

void setScene(SceneGame *scene){myScene = scene;};
    SceneGame* getScene(){return myScene;};

};

player.cpp // Assume player.h is complete

myScene is accessible to any classes derived form Entity

void Player::shootPlayer(float dt){

    // we reduce time for shoot delay

    shootDelay -= dt;

    if (shootDelay < 0.0f) return;

    resetDelay();

    if (Input::instance()->pressKeybutton(sf::Keyboard::Space)){



        // I know I set SceneGame* myScene as protected back in Entity class.
        // However, because it is claimed to be undefined,
        // despite being forward-declared back in entity,
        // I'm getting the 'use of undefined type class' error C2027

        sf::Texture bulletTex;
        bulletTex = (myScene->game->texmgr.getRef("bulletPlayer"));


        Bullet* bullet_p = new Bullet(bulletTex,
            1, 10, false, false, 0.0f);

        bullet_p->setPosition(this->getGlobalBounds().width,
                              this->getGlobalBounds().height / 2);

    }
}

link to answered question: Forward declaration & circular dependency

Community
  • 1
  • 1
JBRPG
  • 141
  • 1
  • 2
  • 10

2 Answers2

1

You have a forward declaration of SceneGame, likely in entity.hpp in the form of class SceneGame;. This is enough to use it as a pointer.

In player.cpp, you actually use the class and you need to know its details ( that you don't need in the header). Likely, your player.cpp should include a SceneGame.hpp (or wherever your SceneGame class is actually defined)

Fix by adding #include "SceneGame.hpp" at the end of the includes in your player.cppfile.

Bruce
  • 7,094
  • 1
  • 25
  • 42
  • Thanks... I realized I do not need to do forward declaration in the player file. I only need the include. Forward declarations are only useful when I have to represent a variable of a particular class. – JBRPG Feb 24 '15 at 00:17
0

Although you have forward declared the SceneGame class, you have to include the full definition before you start using any methods of that class, otherwise, how does the compiler know which methods the SceneGame class supports?

I'd expect that you need to #include "SceneGame.h" or similar in your Player.cpp file.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65