A Problem about istream ">>" overloading

C

Colonel

It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.

// the declaration
friend std::istream & operator>> (std::istream & in, const Complex & a);

// the methods correspond to the friend
std::istream & operator>> (std::istream & in, const Complex & a)
{
std::cout << "real: ";
in >> a.real;
std::cout << "imaginary: ";
in >> a.imaginary;
return in;
}

When complex0.cpp was compiled, such problem appeared:

Compiling...
complex0.cpp
D:\C++\complex\complex0.cpp(45) : error C2679: binary '>>' : no operator
defined which takes a right-hand operand of type 'const double' (or there is
no acceptable conversion)
D:\C++\complex\complex0.cpp(47) : error C2679: binary '>>' : no operator
defined which takes a right-hand operand of type 'const double' (or there is
no acceptable conversion)
Error executing cl.exe.

complex0.obj - 2 error(s), 0 warning(s)

IDE: VC++ 6.0
Can anybody point out the errors with the ">>" overloading?

The overall code is as follows:

// complex0.h -- definition of class Complex
// used for complex operation
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include <iostream>

class Complex
{
private:
double real;
double imaginary;
public:
Complex (); // default constructor
Complex (double r, double i);
~Complex (); // destructor

Complex operator- (const Complex & a) const;
Complex operator- () const;
Complex operator~ () const;

friend std::istream & operator>> (std::istream & in, const Complex &
a);
friend Complex operator+ (const Complex & a, const Complex & b);
friend Complex operator* (const Complex & a, const Complex & b);
friend std::eek:stream & operator<< (std::eek:stream & os, const Complex &
a);
};
#endif

// complex0.cpp -- methods for class Complex
// compiled with complex0.h
#include "complex0.h" // constructors
Complex::Complex ()
{
real = imaginary = 0.0;
}
Complex::Complex (double r, double i)
{
real = r;
imaginary = i;
}
Complex::~Complex () // destructors
{
}

// operators overloading

Complex Complex::eek:perator - (const Complex & a) const // substract
Complex a
{
return Complex (real - a.real, imaginary - a.imaginary);
}

Complex Complex::eek:perator - () const // reverse sign
of Complex
{
return Complex (-real, -imaginary);
}
Complex Complex::eek:perator ~ () const // conjugate
sign of Complex
{
return Complex (real, -imaginary);
}

// friends methods
std::istream & operator>> (std::istream & in, const Complex & a)// input
Complex
{
std::cout << "real: ";
in >> a.real;
std::cout << "imaginary: ";
in >> a.imaginary;
return in;
}
Complex operator+ (const Complex & a, const Complex & b) // plus two
Complex
{
return Complex (a.real + b.real , a.imaginary + b.imaginary);
}

Complex operator* (const Complex & a, const Complex & b) // mutiple
Complex a and b
{
return Complex (a.real * b.real , a.imaginary * b.imaginary);
}

std::eek:stream & operator<< (std::eek:stream & os, const Complex & a) //
display Complex
{
os << "(" << a.real << ", " << a.imaginary << ")";
return os;
}
 
D

Daniel Kay

Colonel said:
It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.

// the declaration
friend std::istream & operator>> (std::istream & in, const Complex & a);

// the methods correspond to the friend
std::istream & operator>> (std::istream & in, const Complex & a)
{
std::cout << "real: ";
in >> a.real;
std::cout << "imaginary: ";
in >> a.imaginary;
return in;
}

When complex0.cpp was compiled, such problem appeared:

Compiling...
complex0.cpp
D:\C++\complex\complex0.cpp(45) : error C2679: binary '>>' : no operator
defined which takes a right-hand operand of type 'const double' (or there is
no acceptable conversion)
D:\C++\complex\complex0.cpp(47) : error C2679: binary '>>' : no operator
defined which takes a right-hand operand of type 'const double' (or there is
no acceptable conversion)
Error executing cl.exe.
>
[ ... ]

Try declaring the second parameter (const Complex &a) as non const.

Regards,
Daniel
 
J

Jim Langston

Colonel said:
It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.

// the declaration
friend std::istream & operator>> (std::istream & in, const Complex & a);

// the methods correspond to the friend
std::istream & operator>> (std::istream & in, const Complex & a)

const means that a won't change.
{
std::cout << "real: ";
in >> a.real;

Ooops, you're trying to change a.
std::cout << "imaginary: ";
in >> a.imaginary;
return in;
}

When complex0.cpp was compiled, such problem appeared:

Compiling...
complex0.cpp
D:\C++\complex\complex0.cpp(45) : error C2679: binary '>>' : no operator
defined which takes a right-hand operand of type 'const double' (or there
is no acceptable conversion)

Adn this is telling you, you 're trying to read into a const double.

[SNIP rest]

Change it to
friend std::istream & operator>> (std::istream & in, Complex & a);
and it should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top