Search code examples
c++eigen

Why errors occur after compilation(c++ eigen) “error C2659: '=' : function as left operand”?


x1.real = x;
x1(k-1).imag = h;
A.col(k-1) = x1.imag / h;

I have written a program of matrix operation with eigen Library in c++,but the error occurred in these lines. How should we correct it?Thanks a lot!

#include "stdafx.h"
#include "iostream"
#include "Eigen/Dense"
using namespace std;
using namespace Eigen;

void jaccsd(Vector3d z, Matrix3d A, Vector3d x)
{
    int m, n,k;
    double h;
    z = x;
    n = 3;
    m = 3;
    A = MatrixXd::Zero(3, 3); 
    h = n*0.0001;
    for (k = 1; k <= n; k++)
    {
        Vector3cd x1;
        x1.real = x;
        x1(k-1).imag = h;
        A.col(k-1) = x1.imag / h; 
    }
}

Solution

  • real and imag are member functions and not data members of Vector3cd, i.e., you need to write

    x1.real() = x;
    

    if you want to assign only the real part of x1. You can also write

    x1 = x;
    

    if you also want to set the imaginary part to zero. In your code the imaginary part would be uninitialized.

    The same goes for:

    A.col(k-1) = x1.imag() / h;