1

I am trying to write some code using Eigen but I am having this issue while trying to compile with g++.

I have the following template definition:

class.h

template<int Nr, int Nz>
Eigen::Matrix<std::complex<double>, Nr, Nz> efield(Eigen::Matrix<double, Nr, Nz>& rs, Eigen::Matrix<double, Nr, Nz>& zs, double t);

class.cpp

Eigen::Matrix<std::complex<double>, Nr, Nz> GaussianBeam::efield(Eigen::Matrix<double, Nr, Nz>& rs, Eigen::Matrix<double, Nr, Nz>& zs, double t) {
//Iterate through the rs, and zs to calculate the field in all combinations of r and z.
    Eigen::Matrix<std::complex<double>, Nr, Nz> output ;
    for (size_t i = 0, size = rs.size(); i < size; i++)
    {
        for (size_t j = 0, sizez = zs.size(); j < sizez; j++) {
            double temporary_r = (*(rs.data() + i));
            double temporary_z = (*(zs.data() + i));

            output(i, j) = this->efield(temporary_r, temporary_z, t);

        }
    }
    return output;
}

And then I call this function from main.cpp:

const int Nx = 3;
const int Ny = 3;
    
//creates the "meshgrid"
Eigen::Matrix<double, Nx, Ny> X = Eigen::RowVectorXd::LinSpaced(Nx, -5e-4,5e-4).replicate(Ny,1);
Eigen::Matrix<double, Nx, Ny> Y = Eigen::VectorXd::LinSpaced(Ny, -5e-3, 5e-3).replicate(1, Nx);

std::cout << "res : \n" << gauss1.efield<3, 3>(X, Y, 0) << "\n";

I am getting the following error on the call to gauss1.efield<3,3> after trying to compile by GCC 5.4.0:

undefined reference to `Eigen::Matrixstd::complex<double, 3, 3, ((Eigen::StorageOptions)0)|((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)1) : ((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)0) : ((Eigen::StorageOptions)0))), 3, 3> Optics::GaussianBeam::efield<3, 3>(Eigen::Matrix<double, 3, 3, ((Eigen::StorageOptions)0)|((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)1) : ((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)0) : ((Eigen::StorageOptions)0))), 3, 3>&, Eigen::Matrix<double, 3, 3, ((Eigen::StorageOptions)0)|((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)1) : ((((3)==(1))&&((3)!=(1)))?((Eigen::StorageOptions)0) : ((Eigen::StorageOptions)0))), 3, 3>&, double)'

The weird thing is that I would have expected perhaps an error on class.cpp but not on main.cpp since this is just a regular call to the function.

I found this error that looks similar to mine, but with an std::vector instead of an std::complex: Stack Overflow, I don't know if it could help. Any ideas why this might be happening?

Btw, I made sure that

#include <complex>
#include <Eigen/Dense>

are in every relevant file.

PS: I am using C++14.

Thank you!

E. Morell
  • 135
  • 2
  • 11
  • In the error, `Eigen::Matrixstd::complex` seems to indicate that you missed `<` somewhere? – chi Aug 01 '20 at 12:31
  • 1
    You need to put the function template definition into the header file or explicitly instantiate all templates you want to call later: https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file – chtz Aug 01 '20 at 12:32
  • Thank you chtz that did it. – E. Morell Aug 01 '20 at 22:46

0 Answers0