-1

I'm having an issue compiling that code. I know the code dose nothing, but it gave me an error saying " use of undefined type Humans, how can I make it work? how can I make classes that can access other classes instances in cpp ? Thank you :)

#include <iostream>

    class Humans;
    class Animals
    {
        
    public:
        
        Animals() :health(1)
        {

        }
        int health;

        void Attack(Humans a)
        {
             a.health = 10;
        }
    };
    class Animals;
    class Humans
    {


    public:
        Humans() :health(0)
        {

        }
        int health;
        void Attack(Animals s)
        {

            s.health = 5;
        }
    };



int main()
{
Humans max;
    Animals lion;
    lion.Attack(max);

}
  • You have to define `Animal::Attack` after `Human` is fully defined. – Scheff's Cat Oct 24 '22 at 09:44
  • Aside: both `Humans::Attack` and `Animals::Attack` have no observable effect, you are modifying local copies, which cease to exist at the `}` – Caleth Oct 24 '22 at 09:45
  • 1
    Get used to not defining member functions inside the class definition. Then you can define all classes before the member functions need them. – molbdnilo Oct 24 '22 at 09:53
  • Circuital dependency in IT of any kind is bad leads to many problems. Anyway why do you have two classes at all? They look identical `Human` `Animal`! So maybe better to have single class `Creature`? – Marek R Oct 24 '22 at 11:11
  • @MarekR , just for testing purposes they can be completely different classes. – Sa'ed Abdelhafez Oct 24 '22 at 11:24
  • @molbdnilo, dose the definition sequence matters when it comes to methods? cant I define my methods anywhere outside my class ? – Sa'ed Abdelhafez Oct 24 '22 at 11:56
  • @Sa'edAbdelhafez See [method 2 here](https://stackoverflow.com/questions/72474030/c-class-declaration-after-using-it). Also refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which are also available as PDFs for free.. – Jason Oct 24 '22 at 13:22

1 Answers1

0

A forward declaration only states that a thing with a particular name exists somewhere; you can't use an instance of it for anything until the definition is also known.

Separate the class definitions and the member function definitions.
(This is the "default" way of writing C++, with class definitions and function definitions in separate files.)

class Human;

class Animal
{
        
public:
    Animal();
    // A forward declaration is enough since we don't 
    // try to use 'a' here.
    void Attack(Human a); 
    int health;        
};

class Human
{
public:
    Human();
    void Attack(Animal s);
    int health;
};

Animal::Animal() : health(1) {}

void Animal::Attack(Human a)
{
    a.health = 10;
}
        
Human::Human() : health(0) {}

void Human::Attack(Animal s)
{
    s.health = 5;
}

(And don't use plural names for singular things. A lion is an animal, not an "animals".)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82