472,371 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 4218
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 &...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.