mercredi 8 avril 2020

Compilation error in C++ while performing operator overloading

I am learning c++ and getting the compilation error while doing operator overloading. I am attaching my code. Please have a look. The error is at line no 27. I tried to make changes but as I am totally new to c++, I am unable to resolve this. We don't have anything like this in C. Also, suggest me some good way to improve my coding standard and skills.

#include <iostream>
#include<string>
#include <string.h>
#include <stdlib.h>

using namespace std;

class rectangle
{
private:
    int l,b;

public:
    // friend

    rectangle(int,int);
    rectangle(rectangle &r);
    void setL(int);
    void setB(int);
    int getL();
    int getB();
    int area();
    int parameter();
    void isSquare();
    rectangle operator + (rectangle r1)
    {
        rectangle temp; // compilation error here ///////

        temp.l=r1.l+l;
        temp.b=r1.b+b;
        return temp;
    }
    // void ~rectangle();
};
void rectangle:: isSquare()
{
    if(l==b)
        cout<<"It is square"<<endl;
    else
        cout<<"It is rectangle"<<endl;
}
rectangle:: rectangle(int length =0, int breadth =0)
{
    setL(length);
    setB(breadth);
}
rectangle:: rectangle(rectangle &r)
{
    setL(r.l);
    setB(r.b);
}

void rectangle::setL(int length)
{
    if(length>0)
        l=length;
    else
        l=0;
}
void rectangle::setB(int breadth)
{
    if(breadth>0)
        b=breadth;
    else
        b=0;
}
int rectangle::getL()
{
    return l;
}
int rectangle::getB()
{
    return b;
}
int rectangle::area()
{
    return l*b;
}
int rectangle::parameter()
{
    return 2*l*b;
}

//extern void print(char *s);
int main()
{
    cout<<("Welcome to test codes!!! :) :) \n");
    rectangle r(10,5);
    rectangle r1(2,5);
    rectangle r2;

    cout<<"r "<<r.area()<<' '<<r.parameter()<<endl;
    cout<<"r1 "<<r1.area()<<' '<<r1.parameter()<<endl;
    cout<<"r2 "<<r2.area()<<' '<<r2.parameter()<<endl;
    r.isSquare();
    r2=r+r1;
    cout<<"r2 "<<r2.area()<<' '<<r2.parameter()<<endl;
    return 0;
}

Error is main.cpp:32:19: error: no matching function for call to 'rectangle::rectangle()' rectangle temp;

Aucun commentaire:

Enregistrer un commentaire