473,804 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

complex number operator question

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:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex , or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there a
way to handle this?

Dec 15 '06 #1
2 1923
jraul wrote:
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:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex , or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there
a way to handle this?
Make sure that the operator* is actually a better choice for the
compiler, i.e. it doesn't have to perform any conversions before
calling it. I think it is possible. Read the FAQ 5.8, I think it
deals with your case.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 15 '06 #2
jraul wrote:
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:

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

complex c = a*b;

Then the compiler complains that it is ambiguous because it doesn't
know if you want complex*complex , or to do the case to double first,
and do double*double. I would think that it would be obvious that
complex*complex should be chosen. Other than explicit casts is there a
way to handle this?
After all I don't think the code above is good.
Though you may find a way to make non-conversion calculation the
"preferred" way for one compiler, you can't ensure that other compilers
accept it. Explicit cast is extremely recommend to prevent ambiguous
overloaded-function call. Scott Meyers mentioned this in his Effective
C++.
So write the cast-constructor as explicit, it is good for your program.

Sincerely,
Z.Z.

Dec 16 '06 #3

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

Similar topics

34
6475
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
3
1839
by: Arthur | last post by:
Spending the morning avoiding responsibilities, and seeing what it would take to color some complex numbers. class color_complex(complex): def __init__(self,*args,**kws): complex.__init__(*args) self.color=kws.get('color', 'BLUE') >>> a=color_complex(1,7) >>> print a
8
2969
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. Now, let's say the user wants to be able to send mailings to people who have various combinations of membership and non-membership in those groups. Here's a medium-complex example: (Knitting Group or Macrame Group) and Active Contact and Mailing...
2
2933
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and imaginary portions. To this end I have some dynamic arrays of doubles to hold the standardized individual rael and imaginary parts. double *ANreal, *ANimag, *BNreal, *BNimag, *CNreal, *CNimag; ANreal = new double; ANimag = new double;
2
9793
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 double() of class B is called for the member complex::operator*=(double) as expected, but not for operator*(complex, double). The effect is, that the template matching (or overload resolution)
7
4393
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. C1(3.2, 1.5) C2(2.1, 4.6) C1+C2 = (5.3, 6.1) How can i overload the operator+ to make an addition between a double and a complex number? for example 3.0 + C1 = (6.2, 1.5) Thanks.
1
1728
by: jraul | last post by:
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
1
9535
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
5
6060
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can use it, but std::sort doesn't seem to find it. I have copied a very simple example below. Everything compiles just fine when the line with std::sort function is commented out, but with that line included a whole slew of errors are given like: ...
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
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 we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.