0

So this is confusing me a little. Neither will be declared before the other, incomplete types or such. Game won't go before Hero because of Hero test1. Hero won't go before Game because of friend void Game::vitals(Hero&);

Is this an impossible circular dependency?

class Game
{
    // ...

    void vitals(Hero&);

    Hero test1;
};

class Hero : public Character
{
    // ...

public:     
    friend void Game::vitals(Hero&);
};
Acorn
  • 24,970
  • 5
  • 40
  • 69
Sean
  • 31
  • 4
  • indeed, circular dependencies. You should consider alternative designs for your classes – Roim May 12 '20 at 09:46

2 Answers2

3

So Hero must go before Game because class Game { ... Hero test1; ... }; requires Hero to be fully defined.

But friend void Game::vitals(Hero&); can simply be reworked as friend class Game;. Making one method of one class as a friend of another is rarely used, and doesn't make much sense since any sort of friendship assumes that the two pieces of code 'trust' each other. So you might as well make the whole classes friends.

The alternative work be to rework the design so that no friendship is needed. It's probably a weakness in your design as it is.

john
  • 85,011
  • 4
  • 57
  • 81
  • It's a project. We have to hammer concepts into this framework of a game. To display understanding. Well, that stumped me a little. But thank you. – Sean May 12 '20 at 09:53
  • @Sean it seems like a bad design to need `Hero` to *know* the `Game`, is it part of the project's guidelines? – Kerek May 12 '20 at 10:13
0

You can either fix this by making Game hold a pointer (or something analogous) to Hero, or by nesting Hero inside Game:

class Game
{
    class Hero;

    void vitals(Hero&);

    class Hero : public Character
    {
        // ...

    public:     
        friend void Game::vitals(Hero&);
    };


    Hero test1;
};
Pubby
  • 51,882
  • 13
  • 139
  • 180