Error about operator overloading when I try to compile this code

new
#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.

Caleth

The compiler is telling you a Rice isn't a Product.

Either you change the return type

Rice operator+(const Product &p)

Or you change the expression you return

{
    Product result = *this;
    result.kg += p.kg; // or some other things?
    return result;
}

Or you re-think your classes. Do you really mean that a Product is a Rice and not the other way around? At the moment a Product has both a kg member directly, and a total_weight member within the Rice base-subobject.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Overloading operator compile error?

Error occurred when I try to compile my code

value.isEmpty is throwing error in when i try to compile the code

I get weird errors about my comments when i try to compile my code using Java 8 and maven 3.2.3

Getting error when overloading << operator

Functions returns error about overloading operator

Whenever I try to compile javascript code I get a syntax error

I get stray '#' in program error when I try to compile this program

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

I'm getting an infinite true bool error when I try to compile this C code and I don't know why

Why there is ambiguous methods error when try to compile code?

Why does the operator overloading fail when i try to sum a number plus a matrix? (but it works the opposite way)

New to c++ getting an error when I try to compile

Gcc throws error when I try to compile the test folder in libburn

when should i use '&' when overloading an operator?

About operator + overloading

"invalid comparator" : error when overloading the "<" operator

why i have this error overloading an operator?

Can I use a flag when overloading an operator?

Error while overloading increment operator inside overloaded I/O operator

Compile time overloading for ostream operator

Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code?

I encounter this error when I try 'pip install StyleFrame', any idea what this error is about?

Pygame window not responding error when i try to run a code

error when I try to activate my code navicat 12

When I try to install vscode, snapd shows error code 400

CSS syntax error in CodeSandBox when i try to write code in a brakets

Confusion about overloading assignment operator

Error when erasing an object element of a vector when operator overloading is defined