为什么编译后会报错(c++ eigen)“error C2659: '=' : function as left operand”?

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;

我用c++的eigen库写了一个矩阵运算的程序,但是错误就出现在这几行。应该怎么修改呢?非常感谢!

#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; 
    }
}

realimag是成员函数而不是Vector3cd的数据成员,即你需要写

x1.real() = x;

如果您只想分配 x1 的实部。你也可以这样写

x1 = x;

如果您还想将虚部设置为零。在您的代码中,虚部将未初始化。

同样适用于:

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