473,387 Members | 1,899 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,387 software developers and data experts.

complex number overload

Suppose we have a complex number class and we overload the conversion
to double to return the real part. We also overload operator* to do
complex multiplication. Consider now:

complex a(...);
complex b(...);

complex c = a*b;

But the compiler complains ambiguity since it doesn't know whether to
do complex*complex, or convert to double first and do double*double.
Other than explicit casting is there a nice solution?

Dec 15 '06 #1
1 1690
On 15 Dec 2006 09:47:08 -0800, "jraul" <jr*******@yahoo.comwrote:
>Suppose we have a complex number class and we overload the conversion
to double to return the real part. We also overload operator* to do
complex multiplication. Consider now:

complex a(...);
complex b(...);

complex c = a*b;

But the compiler complains ambiguity since it doesn't know whether to
do complex*complex, or convert to double first and do double*double.
Other than explicit casting is there a nice solution?
Don't overload the conversion to double, use a.real() and a.imag()
instead. With no conversion to double the compiler only has one
option: complex * complex.

rossum

Dec 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
0
by: Wee Bubba | last post by:
the following event handler checks to see if any parent control has attached to the event 'SearchItemSelect'. i use it for event bubbling whenever one of my search items has been selected and at...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
12
by: vj | last post by:
Hi! I have a piece of code (shown below) involving complex numbers. The code is not running and giving error ("Invalid floating point operation" and "SQRT:Domain error"). I would be very...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
7
by: john134 | last post by:
Complex Complex::operator+( const Complex &operand2 ) const { return Complex( real + operand2.real, imaginary + operand2.imaginary ); } this is my addition operator for 2 complex numbers. e.g....
2
by: PengYu.UT | last post by:
Hi, In FFTW (http://www.fftw.org/), it defines the funciton fftw_malloc to allocate memory properly aligned. However, I only want to use new to allocate memory for std::complex<double>. Can...
2
by: jraul | last post by:
Suppose you have a complex number class, and you overload conversions to double by only taking the real part. You also overload operator* to do complex multiplication. You then write: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.