idikoko
Guest
Posts: n/a

 12-03-2005
I need help overloafing the <,> operators! I wrote a very simple
complex numbers class which basically treats a double as an imaginary
number. I wrote the code to overload the >,< operators but they don't

class Complex {
public:
Complex( double = 0.0); // constructor
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
bool operator>( const Complex & ) const; // Comparision
bool operator<( const Complex & ) const; // Comparision
const Complex &operator=( const Complex & ); // assignment
void print() const; // output
friend ostream &operator<<(ostream&, const Complex &);
friend istream &operator>>(istream&, Complex &);
private:
double imaginary; // imaginary part
};
// Member function definitions for class Complex

// Constructor
Complex::Complex( double i )
: imaginary( i ) { }

Complex Complex:perator+( const Complex &operand2 ) const
{
return Complex(imaginary + operand2.imaginary );
}

Complex Complex:perator-( const Complex &operand2 ) const
{
return Complex( imaginary - operand2.imaginary );
}

bool Complex:perator>( const Complex &right ) const // Comparision
{
return right < *this;
}

bool Complex:perator<( const Complex &right ) const // Comparision
{
return right > *this;
}

const Complex& Complex:perator=( const Complex &right )
{
imaginary = right.imaginary;
}

// Display a Complex object in the form: (a, b)
void Complex:rint() const
{ cout << imaginary << 'i'; }

ostream &operator<<(ostream &output , const Complex &imagine)
{
output<<imagine.imaginary<<"i";
return output;
}

istream &operator>>(istream &input, Complex &i)
{
input>>i.imaginary;
return input;
}

every portion of this code seems to work fine except the <,> parts!

n2xssvv g02gfr12930
Guest
Posts: n/a

 12-03-2005
idikoko wrote:
> I need help overloafing the <,> operators! I wrote a very simple
> complex numbers class which basically treats a double as an imaginary
> number. I wrote the code to overload the >,< operators but they don't
>
>
> class Complex {
> public:
> Complex( double = 0.0); // constructor
> Complex operator+( const Complex & ) const; // addition
> Complex operator-( const Complex & ) const; // subtraction
> bool operator>( const Complex & ) const; // Comparision
> bool operator<( const Complex & ) const; // Comparision
> const Complex &operator=( const Complex & ); // assignment
> void print() const; // output
> friend ostream &operator<<(ostream&, const Complex &);
> friend istream &operator>>(istream&, Complex &);
> private:
> double imaginary; // imaginary part
> };
> // Member function definitions for class Complex
>
> // Constructor
> Complex::Complex( double i )
> : imaginary( i ) { }
>
> Complex Complex:perator+( const Complex &operand2 ) const
> {
> return Complex(imaginary + operand2.imaginary );
> }
>
> Complex Complex:perator-( const Complex &operand2 ) const
> {
> return Complex( imaginary - operand2.imaginary );
> }
>
> bool Complex:perator>( const Complex &right ) const // Comparision
> {
> return right < *this;
> }
>
> bool Complex:perator<( const Complex &right ) const // Comparision
> {
> return right > *this;
> }
>
> const Complex& Complex:perator=( const Complex &right )
> {
> imaginary = right.imaginary;
> return *this; // enables cascading
> }
>
> // Display a Complex object in the form: (a, b)
> void Complex:rint() const
> { cout << imaginary << 'i'; }
>
> ostream &operator<<(ostream &output , const Complex &imagine)
> {
> output<<imagine.imaginary<<"i";
> return output;
> }
>
> istream &operator>>(istream &input, Complex &i)
> {
> input>>i.imaginary;
> return input;
> }
>
>
> every portion of this code seems to work fine except the <,> parts!
>

Your < and > operators form a circular reference where they would be
endlessly calling each other. This is not acceptable and your compiler
having recognised this has refused to allow it.
IMHO complex numbers can have both real and imaginary parts. No doubt
you haven't got round to the real part, preferring the imaginary world
to begin with.

JB

Jonathan Mcdougall
Guest
Posts: n/a

 12-03-2005

idikoko wrote:
> I need help overloafing the <,> operators! I wrote a very simple
> complex numbers class which basically treats a double as an imaginary
> number. I wrote the code to overload the >,< operators but they don't
>
>
> class Complex {
> public:
> Complex( double = 0.0); // constructor
> const Complex &operator=( const Complex & ); // assignment

operator= normally returns a non-const reference.

> Complex operator+( const Complex & ) const; // addition
> Complex operator-( const Complex & ) const; // subtraction
> bool operator>( const Complex & ) const; // Comparision
> bool operator<( const Complex & ) const; // Comparision
> friend ostream &operator<<(ostream&, const Complex &);
> friend istream &operator>>(istream&, Complex &);

Don't make operators friends or member if you don't need to. operator+,
operator- and both stream operators don't need to be members in this
case, as long as you provide a member function returning the imaginary
part.

> void print() const; // output
> private:
> double imaginary; // imaginary part
> };
> // Member function definitions for class Complex
>
> // Constructor
> Complex::Complex( double i )
> : imaginary( i ) { }
>
> Complex Complex:perator+( const Complex &operand2 ) const
> {
> return Complex(imaginary + operand2.imaginary );
> }
>
> Complex Complex:perator-( const Complex &operand2 ) const
> {
> return Complex( imaginary - operand2.imaginary );
> }
>
> bool Complex:perator>( const Complex &right ) const // Comparision
> {
> return right < *this;
> }
>
> bool Complex:perator<( const Complex &right ) const // Comparision
> {
> return right > *this;
> }

Are you sure you want these operators to call themselves recursively?

> const Complex& Complex:perator=( const Complex &right )
> {
> imaginary = right.imaginary;
> return *this; // enables cascading
> }
>
> // Display a Complex object in the form: (a, b)
> void Complex:rint() const
> { cout << imaginary << 'i'; }
>
> ostream &operator<<(ostream &output , const Complex &imagine)
> {
> output<<imagine.imaginary<<"i";
> return output;
> }
>
> istream &operator>>(istream &input, Complex &i)
> {
> input>>i.imaginary;
> return input;
> }
>
>
> every portion of this code seems to work fine except the <,> parts!

Compiles fine for me. The problem is that operator> calls operator<,
which in turn calls operator>. This is inifite recursion and may have
been caught by your compiler and flagged as an error.

Jonathan

Guest
Posts: n/a

 12-03-2005
totally agreed with the infinite recursion part of it. You could implement
one of them as
bool operator<(const Complex& that) const
{
return imaginary < that.imaginary ;
}
and the other one negates this call -
operator
something like
bool operator >(const Complex& that) const
{
!( *this < that ) ;
}

one other point is that operator= return a const reference and this will
not allow cascading .....You just have to return a reference .
Have a nice time !

Jonathan Mcdougall
Guest
Posts: n/a

 12-03-2005
> totally agreed with the infinite recursion part of it.

That may be, but please quote the message you are answering to next
time.

> You could implement
> one of them as
> bool operator<(const Complex& that) const
> {
> return imaginary < that.imaginary ;
> }
> and the other one negates this call -
> operator
> something like
> bool operator >(const Complex& that) const
> {
> !( *this < that ) ;

This is wrong not only because it misses a return, but also because it
would actually return true if *this == that. Make it

return imaginary > that.imaginary;

> }

Jonathan

Chris Theis
Guest
Posts: n/a

 12-03-2005
"maadhuu" <(E-Mail Removed)> wrote in message
> totally agreed with the infinite recursion part of it. You could implement
> one of them as
> bool operator<(const Complex& that) const
> {
> return imaginary < that.imaginary ;
> }
> and the other one negates this call -
> operator
> something like
> bool operator >(const Complex& that) const
> {
> !( *this < that ) ;
> }
>
> one other point is that operator= return a const reference and this will
> not allow cascading .....You just have to return a reference .
> Have a nice time !
>

The negation of less (<) is not greater (>) but greater/equal (>=).
Consequently, your idea will not work as you would expect it. Furthermore,
implementing all those operators considering the imaginary part only does
not really make much sense.

Cheers
Chris

Matteo
Guest
Posts: n/a

 12-05-2005
As others have mentioned, your comparison operators < and > call each
other recursively.

A bigger problem is that when it comes to complex numbers, there is no
well defined comparison between them. You can certainly define one
(comparing their magnitudes, but that has problems since 2 complex
numbers can be different and yet have the same magnitude. Or use
lexicographical ordering, but that is usually useless) but it is of
limited utility.

Complex numbers just don't have a natural comparison, and unless you
have some very good rationale, I would recommend that you resist the
temptation to overload operators < and >.

-matt

p.s. If you do need a comparison so that you can use compex numbers in
a sorted standard container, that's another thing. Myself, I'd use
lexicographical ordering, but I'd put it in a separate comparison
object, and pass it in as a template parameter to the container, rather
than confusing the issue by putting it in the class. But I'm a bit of
a purist, with a math background too.