简体   繁体   中英

"+=" operation not working between types std::complex<double> and __complex__ double

I'm using Eigen to perform some matrix manipulations in C++. In it, I have a line schematically of the form

MatrixXcd A = MatrixXcd::Zeros(10,10);
A(0,0) += 2.0*1i;

Compiling this on my local computer gives no problems. However, compiling it on a different computer using the same CMake file gives the following error:

error: no match for 'operator+=' (operand types are 'Eigen::DenseCoeffsBase<Eigen::Matrix<std::complex<double>, -1, -1>, 1>::Scalar {aka std::complex<double>}' and '__complex__ double')

so somehow the types std::comple<double> and __complex__ double are different, and the computer is unable to resolve the difference. Can someone explain to me what these differences are, and how to remove the discrepancy? I could try figuring out how the two computers are configured differently, but that seems like a more difficult problem to get help with online.

C++ 14 added new literal syntax to make a+bi evaluate to a std::complex<double> .

It should suffice to add

set(CMAKE_CXX_STANDARD 14) # or 17

to your CMakeLists.txt file to make both compilers agree again.

Ensure you add the line

using namespace std::complex_literals;

at beginning of your main() function.

Also check that probably you want to do A(0,0) += 2.0 + 1i; instead of A(0,0) += 2.0*1i;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM