Complex literal 'i' used in function argument

放肆的年华 提交于 2021-02-04 21:58:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!