Reza Abdi - 3 years ago 60
C++ Question

# Fail in working with a Eigen function

The purpose of the mentioned part of my code is solving a linear equation Ax=b for "A" as a square matrix and "b" as a vector with the same size:

The problem is, I get fail in compiling when I try to use the function of the Eigen in my function:

``````#include "Eigen/Dense"
using namespace Eigen;

void StreamTemperature::avgTemperature() {

TotalDist = pm->get_total_dist();
vector<vector<double>> A(TotalDist, std::vector<double>(TotalDist));
vector<double> b_copy;
VectorXd x_eigen;                               //Eigen format!
MatrixXd A_eigen(TotalDist, TotalDist);
VectorXd b_eigen(TotalDist);

for (int i = 0; i < (TotalDist - 1); i++) {
A[i + 1][i] = 1.0;
A[i][i] = 5.0;
A[i][i + 1] = -1.0;
}
A[TotalDist - 1][TotalDist - 1] = b[TotalDist - 1][j] + c[TotalDist - 1][j];
b_copy.resize(TotalDist);
for (int i = 0; i < TotalDist; i++) { b_copy[i] = 10.0; }

for (int i = 0; i < TotalDist; i++)
A_eigen.row(i) = Map<VectorXd>(A[i].data(), TotalTime);
for (int i = 0; i < TotalDist; i++)
b_eigen(i) = b_copy[i];
x_eigen = A_eigen.colPivHouseholderQr().solve(b_eigen);

for (int i = 0; i < TotalDist; i++) {
x[i] = x_eigen(i);
}

}
``````

The Eigen related part work well in anohter project but I have problem here!
Here is the compiling error:

``````1>e:\suny-files\spring2017\summer2017\runforsyracusemin\itreecoolnewsw\eigen\src/Core/PlainObjectBase.h(659): error C2338: FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED
1>e:\suny-files\spring2017\summer2017\runforsyracusemin\itreecoolnewsw\eigen\src/Core/Matrix.h(250): note: see reference to function template instantiation 'void Eigen::PlainObjectBase<Eigen::Matrix<double,-1,-1,0,-1,-1>>::_init2<T0,T1>(int,int,double *)' being compiled
1>        with
1>        [
1>            T0=double,
1>            T1=double
1>        ]
``````

Any thoughts?! :|

When declaring A_eigen , Try to explicitly cast TotalDist to int. The compilation error shows you are passing floating point number where an into is expected.

Also check the value of the TotalDist and the dimension of A_eigen .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download