问题
There seems to be a problem, using the literal i in C++ with std::complex
.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:
error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i
is not convertible to std::complex<double>
?
Do I have to explicitly construct a std::complex
with 1.0i
?
回答1:
You should recompile with --std=c++14
(no GNU ext) to avoid conflict of i
suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the
<complex>
header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
来源:https://stackoverflow.com/questions/51742673/complex-literal-i-used-in-function-argument