Search code examples
c++visual-studio-2010visual-c++new-operatorcomplex-numbers

issues with complex number in VS 2010


#include <complex>
using namespace std;

mx_vector = new double [NX]; //NX = 200
imx_vector = new complex<double> [NX];

i = 1;
imx_vector[i].real () = 0.0;
imx_vector[i].imag () = mx_vector[i]; //mx_vector[i] = 2.2

//This is part of a big program, so hiding the simple details like main() etc.

Hi, I have this code and was working well with intel c++ in redhat linux. Now when I compile it in VS 2010 I get the error "error C2106: '=' : left operand must be l-value". I have looked up the MSDN ref, previous posts on stackflow but couldnt fix it. Is there something to do with "new"?

Any help/reference will be highly appreciated.


Solution

  • imx_vector[i].real() as well as imx_vector[i].imag() return the double, not double&.

    You probably meant (C++98):

    imx_vector[i] = std::complex<double>(0.0, mx_vector[i]);
    

    or (C++11):

    imx_vector[i].real(0.0);
    imx_vector[i].imag(mx_vector[i]);