473,416 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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 4382
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
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
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
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
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
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
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
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
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
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 &...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
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...

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.