0

I'm exercising with understanding classes better. I have a pseudo layout for a game, including an "Engine" class that has a Draw(x, y, ...) method, and a Player class. I also have a "Game" class that simply inherits from Engine and has the GameLoop() method.

My issue is: inside the Game object GameLoop method I want my Player player; to draw himself via a method DrawSelf(this); e.g. player.DrawSelf(this);, effectively passing the Game object by reference and allowing me to define WITHIN the DrawSelf method, how to Draw(x, y, ...).

Essentially I want to be able to call Draw(x,y) from the Player class.

I also have a minor issue at the moment with "Game does not name a type" error, which I assume is something to do with my header declarations, but I am not exactly sure. Here is what I have so far:

engine.h

#ifndef ENGINE_H
#define ENGINE_H

#include <iostream>

class Engine
{
public:
    void Draw(float x, float y);
};

Engine::Draw(float x, float y)
{
    std::cout << "Drawing at (" << x << ", " << y << ")." << std::endl;
}
#endif /*ENGINE_H*/

main.h

#ifndef MAIN_H
#define MAIN_H

#include "engine.h"
#include "player.h"

class Game : public Engine
{
public:
    void GameLoop(int frames);
};

#endif /* MAIN_H */

main.cpp

#include "main.h'

class Game::GameLoop(int frames)
{
    Player player(this); // create player object here and hope to pass Game object to it.

    for (int i = 0; i < frames; i++)
    {
        player.DrawSelf();
    }
};

int main()
{
    Game myGame;
    myGame.GameLoop(5);
    return 0;
}

player.h

#ifndef PLAYER_H
#define PLAYER_H

class Player
{
public:
    class Game; // forward declare to help compiler
    float x, y;
    Game* game; // pointer to game object to be initialized to game object passed in constructor
public:
    Player(game* oGame);
    void DrawSelf();
};

#endif /* PLAYER_H*/

player.cpp

#include "main.h"

Player::Player(Game* oGame)
{
    x = 7.0f;
    y = -2.0f;
    game = oGame; // initializing game object
}

void Player::DrawSelf()
{
    game->Draw(x, y); // trying to use Draw method of Game object inherited from Engine
}
Andrew Cina
  • 134
  • 10
  • It's not obvious to me how that answers my question. I said that was a side point, however my main problem referred to something else. – Andrew Cina Sep 18 '19 at 18:45
  • Regarding *"Game does not name a type"* see [this question](https://stackoverflow.com/questions/11971408/c-circular-header-includes). – François Andrieux Sep 18 '19 at 18:47
  • 1
    I don't understand the first part of your question. It's not clear to me what you are trying to do exactly and what is stopping you. My best guess is that you want to remove `Game* oGame` from `Player`'s constructor and want to add it to it's `DrawSelf` member function instead. Is this correct? – François Andrieux Sep 18 '19 at 18:50
  • `Game does not name a type` could result from a circular dependency in some headers or you used the class before declaring it. But what is your main problem? – Lukas-T Sep 18 '19 at 20:10
  • @François Andrieux, I fixed the circular dependency issue: my problem is that I want a method of A to be available for use by class B without B inheriting from A. If you look at my player.cpp Player class I call Draw(...) which is a method of engine.h Engine class. I've tried an implementation to get it to work, alas it doesn't. So that's the problem I'm looking for a solution for. – Andrew Cina Sep 18 '19 at 20:21
  • you call `game->draw(x, y)` but `Game` does not define such a method. Either you declare such a method in `Game` or if you mean to call `Engine::draw()` you need a pointer to an ìnstance of `Engine`. Or you pass this instance by reference to `Player::DrawSelf`. – Lukas-T Sep 19 '19 at 06:09

0 Answers0