3

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.

Henry Shackleton
  • 351
  • 2
  • 11
  • Is this Eigen related, or do you have the same behavior with just `std::complex a{}; a+=2.0*1i;`? (You should provide a [mre] including what compilers and parameters you are using on each computer) – chtz Dec 10 '19 at 10:27

2 Answers2

2

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.

Botje
  • 26,269
  • 3
  • 31
  • 41
1

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;

Rockcat
  • 3,002
  • 2
  • 14
  • 28