Search code examples
c++eigen

Error storing data from std::Vector to Eigen::Vector


*error: no matching function for call to object of type 'Eigen::VectorXd' (aka 'Matrix<double, Dynamic, 1>')
DenseCoeffsBase.h:362:5: note: candidate function not viable: no known conversion from 'double *' to 'Eigen::Index' (aka 'long long') for 1st argument; dereference the argument with *
DenseCoeffsBase.h:115:41: note: candidate function not viable: no known conversion from 'double *' to 'Eigen::Index' (aka 'long long') for 1st argument; dereference the argument with *
DenseCoeffsBase.h:178:5: note: candidate function not viable: requires single argument 'index', but 2 arguments were provided
DenseCoeffsBase.h:423:5: note: candidate function not viable: requires single argument 'index', but 2 arguments were provided*

The above was the error message provided when I try to store the data from a standard vector to a Eigen Vector

Here's what I'm trying to do. I've made std vectors in other files that I'm returning through those commands and then have to convert them to eigen style vectors to integrate with some other code.I have tried looking for ways to store std vector data into eigen and I found this particular way on another stack overflow post. I dont understand what the error messsage means. Can someone tell me what I'm doing wrong?

Also in an effort to print out the data, I get this error "Reference to non static member function must be called:did you mean to call it with no arguments"

I thought I made it a static vector by resizing.

I'd appreciate help and will add any information necessary. I'm kind of new to C++, and would appreciate a bit simpler explanations as I'm not well versed with all the fundamentals.

    simulator.h

    Eigen::VectorXd currentStartMassVector_, currentEndMassVector_ ,specificImpulses_   ;
    std::vector<double>       StartMassVector_, endMassVector_ , SpecificImpulseVector_  ;

    simulator.cpp

    currentStartMassVector_.resize(numberOfStages_);
    currentEndMassVector_.resize(numberOfStages_);
    specificImpulses_.resize(numberOfStages_);

    StartMassVector_            = launchVehicle->getMassStartStages();
    endMassVector_              = launchVehicle->getMassEndStages();
    SpecificImpulseVector_      = launchVehicle->getCurrentSpecificImpulse();

    currentStartMassVector_(StartMassVector_.data(),StartMassVector_.size())  ;
    currentEndMassVector_(endMassVector_.data(),endMassVector_.size()) ;
    specificImpulses_(SpecificImpulseVector_.data(),SpecificImpulseVector_.size());

        std::cout << "start mass vector" <<  currentStartMassVector_.transpose << std::endl;
        std::cout << "end mass vector"   <<  currentEndMassVector_.transpose << std::endl;
        std::cout << "Isp vector" <<  specificImpulses_.transpose << std::endl;

Solution

  • You can only use the constructor to initialize a vector that was not previously declared. In this case, the Eigen vectors are already declared in the header file.

    Eigen::Map() can be used to copy the data from the std::vector<double> to the Eigen::VectorXd, like this:

    currentStartMassVector_ = Eigen::Map<Eigen::VectorXd>(StartMassVector_.data(),StartMassVector_.size()) ;