0

I am learning class and friend function. I want the function eat() from the class Farm to access the variable price from the class Bull and change the price value. So I need to implement a friend function. I have problem to use function as I do not understand how to use it. I got thoses errors:

gcc friendFClass.cc -lstdc++ -o friendFClass 
friendFClass.cc: In member function 'void Farm::eat(Bull)':
friendFClass.cc:43: error: 'class Bull' has no member named 'eat'
friendFClass.cc: In function 'int main()':
friendFClass.cc:54: error: 'class Bull' has no member named 'eat'

for this program:

#include <iostream>

class Bull; // declaration not full of Bull

class Farm { // declaration full of Farm

    double sizeFarm;
    double massBull;


    public:
    void eat(Bull eatingBull); // Ok Bull was declared 
    void init(double size, double mass) {
        this -> sizeFarm = size;    
        this -> massBull = mass;
    }
    void print() {
        std::cout << "size of the Farm: " << sizeFarm << "\nmass of a bull: " << massBull << std::endl;
    }

};

class Bull {

    int eatBullThisMonth;
    int price;

    friend void Farm::eat(Bull eatingBull); // Ok Farm was full deblared

    public:
    void init(int price) {
        this -> price = price;
    }
    void print() {
        std::cout << "Number of bull for the month: " << eatBullThisMonth << std::endl;
        std::cout << "Total price : " << price << std::endl;
    }

};

void Farm::eat(Bull eatingBull) {
    int pPrice = 12 * eatingBull.price;
    eatingBull.eat(pPrice);
    eatingBull.print();
}

int main() {
    Farm delacroix;
    Bull marguerite;

    delacroix.init(1260, 123);
    marguerite.init(159);

    marguerite.eat(); 

    return 0;

}

I do not know where to define the friend function, inside or outside the class farm and how to define it.
Thank you.

ripeisripe
  • 69
  • 8

2 Answers2

0

Declaring a function as friend in another class, means it can access its private members, not that it inherits the method. In this case it means that Farm::eat has access to Bull private members, but Bull doesn't inherit Farm::eat.

Guillem Castro
  • 271
  • 1
  • 5
0

Problem is ok now as it was a misuse of function and misuse of call of a function. Here is the right code.

#include <iostream>

class Bull; // declaration not full of Bull

class Farm { // declaration full of Farm

    double sizeFarm;
    double massBull;


    public:
    void eat(Bull eatingBull); // Ok Bull was declared 
    void init(double size, double mass) {
        this -> sizeFarm = size;    
        this -> massBull = mass;
    }
    void print() {
        std::cout << "size of the Farm: " << sizeFarm << "\nmass of a bull: " << massBull << std::endl;
    }

};

class Bull {

    int eatBullThisMonth;
    int price;

    friend void Farm::eat(Bull eatingBull); // Ok Farm was full deblared

    public:
    void init(int price) {
        this -> price = price;
    }
    void print() {
        std::cout << "Number of bull for the month: " << eatBullThisMonth << std::endl;
        std::cout << "Total price : " << price << std::endl;
    }

};

void Farm::eat(Bull eatingBull) {
    int pPrice = 12 * eatingBull.price;
    eatingBull.init(pPrice); 
    eatingBull.print();
}

int main() {
    Farm delacroix;
    Bull marguerite;

    delacroix.init(1260, 123);
    marguerite.init(159);

    delacroix.eat(marguerite); 

    return 0;

}
ripeisripe
  • 69
  • 8