operators


class Complex{
public:
Complex(double r=0.0 , double i = 0.0 ) : _r(r) , _i(i){}
void set(double r = 0.0 , double i = 0.0 ) {_r = r; _i = i;}
double getR() const {return _r;}
double getI() const {return _i;}
Complex operator+(const Complex & ) const;
Complex & operator=(const Complex & other);
Complex operator+(const double var);

private:
double _r;
double _i;
};

Complex Complex ::operator+(const Complex & other ) const {
return Complex(_r+other._r, _i+other._i);
}

Complex & Complex::operator=(const Complex & other ) {
_r = other._r;
_i = other._i;
return (*this);
}

Complex Complex::operator+(const double var){
return Complex(_r+var , _i);
}

int main (){

Complex a,b(5.0 , 4.0);
a = 0.4 + b;
}

Hi everyone , when i switch the line "a = 0.4 + b " to " a = b + 0.4 "
the program compiled .
I don't know why the first option doesn't;
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

You defined your operator+ as a member function instead of a free function. Define it as a free function instead and it will allow implicit casts on both sides.
http://stackoverflow.com/q/4622330/1959975
Topic archived. No new replies allowed.