473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Problem about istream ">>" overloading

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
2 4398
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2412
by: Yu Lianqing | last post by:
Hi, all I am writing an overloading operator >> function for a template class and can't make it right. G++ 3.2 (Redhat8.0) gives the following errors: g++ -c list.cxx In file included from...
2
1373
by: ceo | last post by:
Hi there, I'm trying to overload insertion (<<) and extraction (>>) operators and the program yields output I didn't expect. Could someone please clarify what's wrong with my program. Thanks,...
4
1867
by: Don Hedgpeth | last post by:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as. //code for class1 friend std::istream& operator >> (std::istream&...
2
1406
by: Guoqi zheng | last post by:
Dear sir, I am using the default paging function of datagrid. Below is my code. <PagerStyle NextPageText="&gt;&gt;&gt;" PrevPageText="&lt;&lt;&lt;" HorizontalAlign="Center" Mode="NumericPages"></PagerStyle> ...
11
2878
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
2
2116
by: mstearne | last post by:
Has anyone seen any Javascript that mimics the effect that allows you to browse through the New Releases, Just Added sections of the iTunes Music Store? Where you click the arrow icon and the next...
5
2630
by: John Nagle | last post by:
This, which is from a real web site, went into BeautifulSoup: <param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer fantastic rates for selected weeks or days!!&blinkt=Click...
3
2666
by: Demoris | last post by:
I have a class that I believe should work. I've compared it to previous programs I've written, compared it to examples from my professor, to examples in my textbook, but can't see why I get the...
0
340
by: Colonel | last post by:
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 &...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.