This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Calling external functions taking eigen types as parameters

Tags: None
(comma "," separated)
jerco
Registered Member
Posts
3
Karma
0
Hi,

I am facing problems when I want to call functions using eigen types as parameters. My final goal will be to create a project with the following structure:
A 'main.cpp' using a function_1 written outside the main (with .cpp and .h files). 'function_1.ccp' uses also an external function 'function_2' (written in .cpp and .h files)
I have succeeded to do that using as parameters of the functions simple types (int, float, ...) but I can't reproduce this method for eigen types parameters. I have looked to Eigen documentation 'Writing Functions Taking Eigen Types as Parameters' but It has not helped me since the documentation doesn't precise how to do with external functions.

First I am trying to do that with a fake example. The minimal structure of my code is the following:
main.cpp
Code: Select all
#include <iostream>
#include <Eigen/Core>
#include "function_1.h"
uing namespace Eigen;
using namespace std;

int main()
{
VectorXd v;
function_1(v);
}


function_1.cpp
Code: Select all
#include <iostream>
#include <Eigen/Core>
#include "function_1.h"
#include "function_2.h"
uing namespace Eigen;
using namespace std;

template <typename Derived>
void function_1(Eigen::MatrixBase<Derived>& v)
{
 v = VectorXd::LinSpaced(11,0,10);
cout << v << endl;

VectorXd w;
function_2(w);
}


function_1.h
Code: Select all
#ifndef FUNCTION_1_H_INCLUDED
#define FUNCTION_1_H_INCLUDED
#include <Eigen/Core>

uing namespace Eigen;
using namespace std;

template <typename Derived>
void function_1(Eigen::MatrixBase<Derived>& v);

#endif //FUNCTION_1_H_INCLUDED


function_2.cpp
Code: Select all
#include <iostream>
#include <Eigen/Core>
#include "function_2.h"
uing namespace Eigen;
using namespace std;

template <typename Derived>
void function_2(Eigen::MatrixBase<Derived>& v)
{
 v = VectorXd::LinSpaced(11,0,10);
cout << v << endl;
}


function_2.h
Code: Select all
#ifndef FUNCTION_2_H_INCLUDED
#define FUNCTION_2_H_INCLUDED
#include <Eigen/Core>

uing namespace Eigen;
using namespace std;

template <typename Derived>
void function_2(Eigen::MatrixBase<Derived>& v);

#endif //FUNCTION_2_H_INCLUDED


I have the following error:
In function 'main'
undefined reference to 'void fonction_1<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1 ,0, -1, 1> >&)


Thank you for your help,

Julien
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Template functions have to be defined in header files or explicitly instantiated for all possible template parameters. So if these two solutions are not suitable for you, you can use a Ref<VectorXd> type for you parameter. Compared to a simple VectorXd, this will give you more flexibility on the set of arguments that will be accepted without copy. See: http://eigen.tuxfamily.org/dox-devel/cl ... _1Ref.html for more details.
jerco
Registered Member
Posts
3
Karma
0
Thanks the Ref<Vector> works fine for me.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]