Search code examples
c++operator-overloadingfriend

Friendship and Operator+= overloading


So I've been trying to over load the += operator but it isn't working in the Class Item I try to make only the += operator a friend but it says that members are still in accessible(the Commented line) my first question is why doesnt this work, so i make the whole Order class a friend , now it can access the data member however the overloading still doesnt work

Here's my Code : Item Class

#pragma once
#include"Order.h"
class Item
{
    double Iprice;
public:
    friend Order ;
    //friend Order& Order::operator+=(Item);
    Item(double p =0):Iprice(p){

    }

    ~Item(void);
};

Order Class :

#pragma once
#include"Item.h"
#include<iostream>
using namespace std;
class Order
{
    double Oprice;
    int n_items;
    friend ostream& operator<<(ostream& c , Order O );
public:

    Order(double price =0, int n=0 ){
        Oprice = price;
        n_items = n;
    }

    Order& operator+=(Item I1){
        n_items++;
        Oprice += I1.Iprice;

        return (*this);
    }

};

Main function :

#include<iostream>
#include"Item.h"
#include"Order.h"
using namespace std;


ostream& operator<<(ostream& c , Order O ){
    c<< "Total Price = " << O.Oprice << "-- item Count = " << O.n_items << endl ;

    return c ;

}
int main(){

Item t1;  
t1 = 1000;

Item t2 = 100;
Item t3 = 10;

Order ord1; 

Order ord2;
cout << (ord1 += t1);

}

Thanks in advance


Solution

  • The problem is 'Item.h' is trying to include 'Order.h' and 'Order.h' is trying to include 'Item.h' which is creating a loop.

    Item.h -> Order.h -> Item.h -> Order.h -> ...
    

    This is the reason why there is no proper definition for classes Item and Order and compiler is throwing error when you are trying to compile.

    To solve this issue, you can forward declare Object class in Item.h as you are not using Order class inside Item.h other than just declaring as friend. A forward declaration tells the compiler about the existence of an entity before actually defining the entity. There are some other errors like no definition for Item class destructor. I fixed those and Code is as below.

    Item.h

    class Order;
    
    class Item
    {
        double Iprice;
    public:
        friend Order;
        Item(double p =0):Iprice(p){
    
        }
    
        ~Item()
        {
        }
    };
    

    Order.h

    #pragma once
    #include"Item.h"
    #include<iostream>
    using namespace std;
    
        class Order
        {
            double Oprice;
            int n_items;
            friend ostream& operator<<(ostream& c , Order O );
        public:
    
            Order(double price =0, int n=0 ) {
                Oprice = price;
                n_items = n;
            }
    
            Order& operator+=(Item I1) {
                n_items++;
                Oprice += I1.Iprice;
    
                return (*this);
            }
        };
    

    main.cpp

    #include<iostream>
    //#include "Item.h" // No need to include Item.h here as Order.h in the next line internally includes Item.h
    #include "Order.h"
    using namespace std;
    
    ostream& operator<<(ostream& c , Order O ){
        c<< "Total Price = " << O.Oprice << "-- item Count = " << O.n_items << endl ;
    
        return c ;
    
    }
    int main(){
    
    Item t1;
    t1 = 1000;
    
    Order ord1;
    
    cout << (ord1 += t1);
    }
    

    Output :

    Total Price = 1000-- item Count = 1