Operator like " >>,<<" overloading does not work

3

Here is My Code

#include<iostream>
using namespace std;
class Complex
{
private:
    double re;
    double im;
public:
    Complex(double x=0,double y=0):re(x),im(y){}
    friend ostream& operator<<(ostream&os,const Complex&a);
    friend istream& operator>>(istream &is,const Complex&a);
};
ostream& operator<<(ostream&os,const Complex&a)
{
    os<<"a"<<a.re<<"+j"<<a.im<<endl;
    return os;
}
istream& operator>>(istream&is,const Complex&a)
{
    is>>a.re>>a.im;
    return is;
}
int main()
{
    Complex d;
    cout<<"enter the value of object";
    cin>>d;
    cout<<"value is"<<d;
    return 0;
}

When i execute program runs and stop automaticaly It gives the following as output

Process returned -1073741571 (0xC00000FD) execution time : 3.066 s Press any key to continue.

c++
asked on Stack Overflow Apr 9, 2020 by naman singh

2 Answers

5

The primary problem is that you made the second parameter of operator>> const. A const reference cannot be used to modify the object, but operator>> should do that.

Now, the question is, why does it compile at all? The expectation would be that there is no viable overload of operator>> that takes a const double& (the type of a.re and a.im in your operator>> overload) as second argument. There is only a double& overload in the standard library. So how does is>>a.re>>a.im; not cause a compilation failure?

That is because you made the constructor of Complex non-explicit and callable with exactly one argument of type double.

Therefore each >> in

is>>a.re>>a.im;

will actually call your operator>> overload again, by doing a user-defined conversion of the a.re or a.im argument from double to Complex, using the mentioned non-explicit constructor and that Complex can bind to the const reference of operator>> again.

You should make constructors that can be called with exactly one argument explicit by default if you don't have any specific reason to do otherwise, to avoid such issues. The explicit keyword forbids the constructor from being used in implicit conversion sequences as above, which are often surprising and unintentionally.

So effectively you have an infinite recursion in operator>> (and your compiler may tell you about this if you enable warnings). This causes undefined behavior in theory and a stack overflow causing a segmentation fault in practice.

answered on Stack Overflow Apr 9, 2020 by walnut • edited Apr 9, 2020 by walnut
1

OK the problem with above code is that i used >> operator on const object so the actual code goes like

#include<iostream>
using namespace std;
class Complex
{
private:
    double re;
    double im;
public:
    Complex(double x=0,double y=0):re(x),im(y){}
    friend ostream& operator<<(ostream&os,const Complex&a);
    friend istream& operator>>(istream &is,Complex&a);
};
ostream& operator<<(ostream&os,const Complex&a)
{
    os<<"a"<<a.re<<"+j"<<a.im<<endl;
    return os;
}
istream& operator>>(istream&is,Complex&a)//here was the error
{
    is>>a.re>>a.im;
    return is;
}
int main()
{
    Complex d;
    cout<<"enter the value of object";
    cin>>d;
    cout<<"value is"<<d;
    return 0;
}
answered on Stack Overflow Apr 9, 2020 by naman singh

User contributions licensed under CC BY-SA 3.0