2

I need to make no bigger project "Army", everyone has their own abilities, etc. But for some reason, when compiling, I write that I did not declare SpellCaster and Unit. And I cannot understand where I need to declare them, or in what error?

In file included from Unit.h:5:0,
                 from Unit.cpp:1:
SpellCaster.h:25:22: error: 'Unit' has not been declared
  virtual void attack(Unit& enemy);
                      ^~~~
In file included from SpellCaster.h:6:0,
                 from SpellCaster.cpp:1:
Unit.h:28:25: error: 'SpellCaster' has not been declared
     virtual void attack(SpellCaster& spellcaster);

This is my Unit.h :

#include <iostream>
#include "SpellCaster.h"

class UnitIsDead {};

class Unit {
private:
    int hpLimit;
    int dmg;
    int hp;
    std::string name;
    void ensureIsAlive();
public:
    Unit(int hpLimit, int dmg, const std::string& name);
    ~Unit();

    int getDamage() const;
    int getHP() const;
    int getHpLimit() const;
    const std::string& getName() const;

    void takeDamage(int dmg);

    virtual void attack(Unit& enemy);
    virtual void attack(SpellCaster& spellcaster);
    void counterAttack(Unit& enemy);
};

and my SpellCaster.h :

#include "Unit.h"

class SpellCaster{
private:
    int hp;
    int dmg;
    int hpLimit;
    std::string name;
public:
    SpellCaster(int hpLimit, int dmg, const std::string& name);
    ~SpellCaster();

    int getDamage() const;
    int getHP() const;
    int getHpLimit() const;
    const std::string& getName() const;

    void takeDamage(int dmg);
    virtual void attack(SpellCaster& spellcaster);
    virtual void attack(Unit& enemy);

};

I'll be very thankful

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
egor
  • 25
  • 6
  • 1
    Likely a problem with your `#include` statements. Edit: I see the problem. Unit.h includes SpellCaster.h and SpellCaster.h includes Unit.h. that won't work. You need to break the loop. – drescherjm Oct 21 '21 at 14:49
  • Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Oct 21 '21 at 14:50
  • You could remove `#include "Unit.h` from `SpellCaster.h` and `#include ` from `Unit.h` and forward declare: `class Unit;` in `SpellCaster.h` and.`class SpellCaster;` in `Unit.h` - both above the class definitions that needs them. – Ted Lyngmo Oct 21 '21 at 14:50
  • The file Unit.h should not include SpellCaster.h, instead should have a foward declaration to SpellCaster. The file SpellCaster.h should not include Unit.h, instead should have a forward declaration to Unit. – Eljay Oct 21 '21 at 14:56
  • @egor No, like I did in my answer. – Ted Lyngmo Oct 21 '21 at 15:11
  • Is there a missing inheritance relationship here? That will constrain how you have to break the circular dependency. – Ben Voigt Oct 21 '21 at 15:46
  • @egor Did the suggestions help or is BenVoigt on to something? Is there something more to this that you haven't shown us? – Ted Lyngmo Oct 22 '21 at 06:12

1 Answers1

1

Suggestions:

Unit.h:

#include <iostream>
// #include "SpellCaster.h"  // remove this
class SpellCaster;           // and add a forward declaration instead

SpellCaster.h

// #include "Unit.h"         // remove this
class Unit;                  // and add a forward declaration instead

You should also add header guards to both header files. Most compilers support putting

#pragma once

as the first line in the header files. If your's don't, surround all the code in the header files with:

#ifndef FILENAME_H_SOMETHING  // UNIT_H_HEADER_GUARD or SPELLCASTER_H_HEADER_GUARD
#define FILENAME_H_SOMETHING

// the current content of the file

#endif
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108