Search code examples
c++constructoreigen

Eigen::VectorXd constructor desires MatrixXd when compiling


Have a straightforward problem with testing out some Eigen functionality. I'm creating a constructor that takes 3 Eigen::Vector by reference. When I construct those 3 in my main and call the Interp object (the class I created) constructor, I get that the constructor desires Eigen::MatrixXd (see the compile error at the bottom of this question). My code is shown below. I haven't added much functionality yet so I'd say it's a pretty bare-bone problem but I can't find a solution myself.

main.cpp

#include "Interp.hpp"

int main(){
    Eigen::VectorXd xData(4);
    xData << 0.0, 0.33, 0.66, 1.00;
    Eigen::VectorXd zData(4);
    zData << 0.0, 1.0, 0.5, 0.0;
    Eigen::VectorXd dzData(4);
    dzData << 0.0, 0.0, 0.0, 0.0;

    Interp obj(xData,zData,dzData);
}

Interp.hpp

#include <iostream>
#include <vector>
#include <eigen3/Eigen/Dense>

class Interp {
public:
    Interp(Eigen::VectorXd &xData, Eigen::VectorXd &zData, Eigen::VectorXd &dzData);
    void compute();
    double evaluate(double &x);
    Eigen::VectorXd evaluate(Eigen::VectorXd &x);

private:
    Eigen::VectorXd xData;
    Eigen::VectorXd zData;
    Eigen::VectorXd dzData;
    Eigen::MatrixXd xPoints;
    
    Eigen::VectorXd p;
    Eigen::MatrixXd A;
    Eigen::MatrixXd b;
};

Interp.cpp

#include "Interp.hpp"

Interp::Interp(Eigen::VectorXd &xData, Eigen::VectorXd &zData, Eigen::VectorXd &dzData){
    this->xData = xData;
    this->zData = zData;
    this->dzData = dzData;
    this->xPoints.resize(xData.size()-1,2);
}

And I build using CMake, so my CMakeLists.txt file is: CmakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(test1 VERSION 0.1.0)

include(CTest)
enable_testing()

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

add_executable(test1 main.cpp)

target_link_libraries (test1 Eigen3::Eigen)


set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

The output I get is as follows:

[main] Building folder: test 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/none/test/build --config Debug --target all -- -j 10
[build] Scanning dependencies of target test1
[build] [ 50%] Building CXX object CMakeFiles/test1.dir/main.cpp.o
[build] [100%] Linking CXX executable test1
[build] CMakeFiles/test1.dir/main.cpp.o: In function `main':
[build] /home/none/test/main.cpp:11: undefined reference to `Interp::Interp(Eigen::Matrix<double, -1, 1, 0, -1, 1>&, Eigen::Matrix<double, -1, 1, 0, -1, 1>&, Eigen::Matrix<double, -1, 1, 0, -1, 1>&)'
[build] collect2: error: ld returned 1 exit status
[build] CMakeFiles/test1.dir/build.make:94: recipe for target 'test1' failed
[build] make[2]: *** [test1] Error 1
[build] make[1]: *** [CMakeFiles/test1.dir/all] Error 2
[build] CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test1.dir/all' failed
[build] Makefile:116: recipe for target 'all' failed
[build] make: *** [all] Error 2
[build] Build finished with exit code 2

Where roughly in the middle you would see that there is an undefined reference to a Matrix constructor. I understand that a VectorXd is an inherited type of a Matrix but I did not expect that this would give me any trouble when calling the constructor. It is still its own type after all.

So quite a straightforward problem but I'm at a loss on what to do. Any help would be appreciated.


Solution

  • For the answer, please see @rafix07 in the comments below my initial question:

    "You are not compiling

    Interp.cpp

    try

    add_executable(test1 main.cpp interp.cpp)"

    -- rafix07