0

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 ///////Line No 27:

        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;

ABD
  • 180
  • 11
  • And which line, exactly, is #32? Please create a [mcve], don't just deposit your code files here. – Murphy Apr 08 '20 at 11:40
  • 1
    Also, it would be better to declare your operator as `rectangle operator + (const rectangle& r1) const` – Jasper Kent Apr 08 '20 at 11:43
  • Default values for parameters go in DECLARATION, not definition of method. Just move `=0,=0` into declaration of constructor. – rafix07 Apr 08 '20 at 11:51

2 Answers2

3

Look at your constructors

rectangle(int,int);
rectangle(rectangle &r);

One that takes two integers, and one that takes another rectangle. So this means to construct a rectangle you must provide either two integers or another rectangle.

Now look at the line with the error

rectangle temp;

What's provided here? Nothing of course, but you don't have a constructor with no arguments, so this is a compilation error.

Looking at your code the best solution is this

    rectangle temp(r1.l+l, r1.b+b);
    return temp;

Now two integers are being provided.

BTW your code shows a complete lack of awareness of const. You're going to quickly run into trouble unless you learn about that.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    I got it. Thanks for quick and helpful reply. Can you please explain more on complete lack on const? I did not get you. – ABD Apr 08 '20 at 11:54
  • @Akhilesh see https://isocpp.org/wiki/faq/const-correctness#overview-const and https://stackoverflow.com/questions/136880/sell-me-on-const-correctness (the latter of which links to the former). Using `const` lets the compiler produce more efficient code, and it tells other people who maintain your code (including future you) what your code can and can't do. – JohnFilleau Apr 08 '20 at 12:04
1

The line

rectangle temp; // compilation error here ///////

is trying to create a new instance of "rectangle" without any constructor parameters. Your class does not have a constructor that takes no parameters, that's why you get the error. It has nothing to do with your operator.

You need to use:

rectangle temp(0, 0);

Or, create a constructor that takes no parameters:

in header

rectangle();

in source file

rectangle:: rectangle() {}
birgersp
  • 3,909
  • 8
  • 39
  • 79