mardi 8 mai 2018

Template fucntion using Eigen header

I want to write a function that computes the norm of an Vector defined with Eigen. Minimum working example:

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

using namespace Eigen;

template<typename t>

t norm( Matrix<t,Dynamic,1> RR ){

    t result = ( t ) 0;
    for ( auto i = 0 ; i < RR.rows(); i++ )
            result += RR( i ) * RR( i );
    }

    return result;
}


int main(){




    Matrix<float , 3 , 1 > test;
    test << 1,2,3;

    std::cout << test << std::endl;
    std::cout << norm( test ) << std::endl;}

If I compile this code I get the following Error:

chem:~/programs/cpp/charge_ana> g++ -std=c++11 test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:28:26: error: no matching function for call to    
‘norm(Eigen::Matrix<float, 3, 1>&)’
std::cout << norm( test ) << std::endl;
test.cpp:28:26: note: candidate is:
test.cpp:9:3: note: template<class t> t norm(Eigen::Matrix<Scalar, -1, 1>)
t norm( Matrix<t,Dynamic,1> RR ){
^
test.cpp:9:3: note:   template argument deduction/substitution failed:
test.cpp:28:26: note:   template argument ‘3’ does not match ‘#‘integer_cst’ not supported by dump_decl#<declaration error>’

std::cout << norm( test ) << std::endl; ^ test.cpp:28:26: note: ‘Eigen::Matrix’ is not derived from ‘Eigen::Matrix’

Does anybody has a hint what to do. Because I neither know what else to try nor do I really understand the error message during compilation

Aucun commentaire:

Enregistrer un commentaire