473,574 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<complex> : no match for 'operator*' // conversion operator double()

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::operat or*=(double) as expected, but not for operator*(compl ex,
double).

The effect is, that the template matching (or overload resolution)
fails. Error message:
complex_double_ conversion.cpp: In function 'int main()':
complex_double_ conversion.cpp: 19: error: no match for 'operator*' in 'z
* x'

Tested with g++ 4.0.2 20050901 (prerelease) on (SUSE Linux) and others.
Do <you> know a reason/remedy for this behaviour? Thanks in advance.

//------------------------complex_double_ conversion.cpp-------------------------
#include<comple x>

class B {
double v;
public:
operator double() const { return v; } // conversion operator
B(double _v) : v(_v) {}
};

int main() {
std::complex<do uble> z(0,1);
B x(0.5);

// next line works due to complex<_Tp>&
complex<_Tp>::o perator*=(const _Tp&)
// the conversion operator of class B is used
z*=x;

// the next line does not compile
std::complex<do uble> y( z*x );

// only with cast: z*((double) x)
//
// although in <complex> there is
// template<typena me _Tp>
// inline complex<_Tp> operator*(const complex<_Tp>& __x, const
_Tp& __y)
// { return complex<_Tp> (__x) *= __y; }
//
return 0;
}
//-----------------------------------------------------------------------------------------

Jun 23 '06 #1
2 9777
Arvid Requate wrote:
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::operat or*=(double) as expected, but not for operator*(compl ex,
double).

The effect is, that the template matching (or overload resolution)
fails. Error message:
complex_double_ conversion.cpp: In function 'int main()':
complex_double_ conversion.cpp: 19: error: no match for 'operator*' in 'z
* x'

Tested with g++ 4.0.2 20050901 (prerelease) on (SUSE Linux) and others.
Do <you> know a reason/remedy for this behaviour? Thanks in advance.
The operator* function you want to call is a non-member as follows:
template<class T>
complex<T> operator*(const complex<T>& lhs, const T& rhs);

Template argument deduction fails because it requires that the arguments
are exact matches for the deduced parameter types (with a few
exceptions), but B is not similar enough to double const& (user defined
conversions aren't considered).

The easiest fix is to write a suitable function (in the same namespace
as B so that it is found by ADL), something like:

complex<double> operator*(const complex<double> & lhs, const B& rhs)
{
return lhs * static_cast<dou ble>(rhs);
}

Tom

//------------------------complex_double_ conversion.cpp-------------------------
#include<comple x>

class B {
double v;
public:
operator double() const { return v; } // conversion operator
B(double _v) : v(_v) {}
};

int main() {
std::complex<do uble> z(0,1);
B x(0.5);

// next line works due to complex<_Tp>&
complex<_Tp>::o perator*=(const _Tp&)
// the conversion operator of class B is used
z*=x;

// the next line does not compile
std::complex<do uble> y( z*x );

// only with cast: z*((double) x)
//
// although in <complex> there is
// template<typena me _Tp>
// inline complex<_Tp> operator*(const complex<_Tp>& __x, const
_Tp& __y)
// { return complex<_Tp> (__x) *= __y; }
//
return 0;
}
//-----------------------------------------------------------------------------------------

Jun 23 '06 #2
* Arvid Requate:

I'd like to understand why the following code does not compile. [snip]
//------------------------complex_double_ conversion.cpp-------------------------
#include<comple x>

class B {
double v;
public:
operator double() const { return v; } // conversion operator
B(double _v) : v(_v) {}
};

int main() {
std::complex<do uble> z(0,1);
B x(0.5);

// next line works due to complex<_Tp>&
complex<_Tp>::o perator*=(const _Tp&)
// the conversion operator of class B is used
z*=x;

// the next line does not compile
std::complex<do uble> y( z*x );

// only with cast: z*((double) x)
//
// although in <complex> there is
// template<typena me _Tp>
// inline complex<_Tp> operator*(const complex<_Tp>& __x, const
_Tp& __y)
// { return complex<_Tp> (__x) *= __y; }
//
return 0;
}
//-----------------------------------------------------------------------------------------


Template parameter matching does not consider user-defined conversions:
in general types must match exactly (sort of, there's a bit of looseness
in cv-qualification and reference types and so on).

Consider:

template< typename T >
struct Complex { Complex( T = 0, T = 0 ) {} };

template< typename T >
Complex<T> operator*( Complex<T> const&, Complex<T> const& )
{ return Complex<T>(); }

class B
{
double v;
public:
B( double _v ) : v( _v ) {}
operator Complex<double> () const { return v; }
};

int main()
{
Complex<double> z(0,1);
B x(0.5);

// The next line does not compile, not exact match:
z*x;
}

However, adding

template< typename T >
Complex<T> operator*( Complex<T> const& a, B const& b )
{ return a*Complex<T>(b) ; }

provides an exact match and the code compiles.

A bit more general, adding instead

template< template<class> class C, typename T >
C<T> operator*( C<T> const& a, B const& b )
{ return a*C<T>(b); }

also provides an exact match and the code compiles.

I tried this fix with your original code and it compiles with MSVC 7.1.
However, you'll probably also have to support the opposite argument
order, and other operators such as '+'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 23 '06 #3

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

Similar topics

2
1751
by: Peng Yu | last post by:
http://www.digitalmars.com/rtl/complex.html The above the specification of complex.h, but it seem that g++(3.3) hasn't fully support it yet. I wonder where I can find the specifications of complex.h supported by old version g++ (say 2.95.4). Peng
7
4118
by: Don Tucker | last post by:
Hello, I have the following test program that I can compile with gcc, but when I try to compile with the portland group compiler, pgcc, I get a stream of errors. #include <stdio.h> #include <complex.h> int main(int argc, char **argv){ complex double a={2.3+1.4*_Complex_I}; printf("a=%lf + i%lf\n",creal(a),cimag(a));
17
3713
by: Julian V. Noble | last post by:
Dear C Mavens, I am writing a Computing Prescription for CiSE on complex arithmetic. I would like to be sure that I do not include any gaffes about C99--especially about what is in the header file <complex.h. I have Googled <complex.hand got about 177K hits, which I obviously do not wish to go through one at a time. I also tried...
3
3541
by: Klaas Vantournhout | last post by:
Hi, I am using CLAPACK to do lots of matrix operations, but this is done on complex matrices. There I also need to work with normal complex operators, I use also the standard complex library. Unfortunately this one conflicts with some stuff in f2c.h needed to run CLAPACK. Examples are that f2c.h declares a variable complex that fights...
3
6148
by: J.M. | last post by:
I have data in a double array of length 2N, which actually represents complex numbers with real and imaginary parts interlaced. In other words, elements in this array with even indices represents the real part of a complex number, elements with odd indices represent the imaginary part. I actually need an array of length N of type...
5
6042
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...
0
7804
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...
0
8232
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...
1
7811
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6451
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...
1
5620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5300
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2241
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
1
1339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1057
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...

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.