Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

eigen

eigen3

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.

like image 437
Henry Shackleton Avatar asked Oct 20 '25 03:10

Henry Shackleton


2 Answers

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;

like image 162
Rockcat Avatar answered Oct 24 '25 08:10

Rockcat


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.

like image 39
Botje Avatar answered Oct 24 '25 07:10

Botje