error: no matching function for call to ‘ope::ope()

Viewed 42
 #include <iostream>
        using namespace std;
        class ope
        {
            private: int real, imag;
            public:
            ope(int r, int i){
                    real=r;
                    imag=i;
         }
        ope operator + (ope const &obj) //operator overloading
            {   ope temp;
                temp.imag = imag + obj.imag;
                temp.real = real + obj.real;
                return temp;
            }
            void show()
            {
                cout << "Result : " << real << " + i"  << imag << endl;//print complex numbers
            }
        };
        int main()
        {
            ope f1(2,5) , f2(7,6);
            ope f3 = f1+f2;
            f3.show();
        return 0;
        }

I am new in programming and I tried to use operator overloading but I got this error can anyone help me? In this code, I am trying to print complex numbers using operator overloading.

1 Answers

The line

ope temp;

requires a parameterless constructor (a.k.a default constructor), but ope has only a constructor that requires two parameters. You might as well use the parameterized constructor here:

ope operator + (ope const &obj) //operator overloading
{   
    ope temp(real + obj.real, imag + obj.imag);

    return temp;
}

... or define a default constructor

class ope {
// ...
public:
  ope(): real(0), imag(0) {}
// ...
Related