By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

A Problem about istream ">>" overloading

P: n/a
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::ostream & operator<< (std::ostream & 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::operator - (const Complex & a) const // substract
Complex a
{
return Complex (real - a.real, imaginary - a.imaginary);
}

Complex Complex::operator - () const // reverse sign
of Complex
{
return Complex (-real, -imaginary);
}
Complex Complex::operator ~ () 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::ostream & operator<< (std::ostream & os, const Complex & a) //
display Complex
{
os << "(" << a.real << ", " << a.imaginary << ")";
return os;
}

Sep 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Colonel schrieb:
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
Sep 29 '07 #2

P: n/a

"Colonel" <xi**********@163.comwrote in message
news:fd**********@news.cn99.com...
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.
Sep 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.