mercredi 6 avril 2022

I am getting an error about operator overloading error when I try to compile this code, please help me, thanks a lot

#include<iostream>
#include<iomanip>
using namespace std;
class Rice
{
    float price_per_kg, total_weight;
    public:
    Rice(float w)
    {
        price_per_kg = 10.0;
        total_weight = w;
    }
    void display_rice()
    { 
        cout<<"----------------------------------------"<<endl;
        cout<<"\tRice Details"<<endl;
        cout<<fixed<<setprecision(2);
        cout<<"Total weight\t : "<<total_weight<<endl;
        cout<<"Price perkg (RM): "<<price_per_kg<<endl;
        cout<<"Total (RM)\t : "<<total_weight*price_per_kg<<endl;
    }
};

class Product:public Rice
{
    float kg;
    public:
        Product operator+(const Product &p)
        {
            return Rice(kg + p.kg);
        }
        void Setdata()
        {
            cout << "Enter product's weight(kg): ";
            cin >> kg;
        }
};

int main()
{
    Product a, b;
    a.Setdata();
    b.Setdata();
    Rice h = a + b;
    h.display_rice();
}

I got this error: [Error] could not convert 'Rice((((Product*)this)->Product::kg + ((float)p.Product::kg)))' from 'Rice' to 'Product'

Is there any way to solve the error, I have tried to use other ways to solve but the question requires operator overloading to complete the question

Aucun commentaire:

Enregistrer un commentaire