Search code examples
c++eigeneigen3spectra

Compilation error C2664 and C2440 when constructing a template function in C++


I am using Spectra C++ library, which is built on top of the Eigen C++ library.

I will need to construct a correct SymGEigsSolver object based on the given the SelectionRule and GeigsMode parameters in a method. The definition of SymGEigsSolver is as follows:

template<typename Scalar, int SelectionRule, typename OpType, typename BOpType, int GEigsMode>
class Spectra::SymGEigsSolver< Scalar, SelectionRule, OpType, BOpType, GEigsMode >

However when I tried to compile the below function:

typedef SparseMatrix<double, Eigen::RowMajor> SpMat;
typedef Spectra::SparseSymMatProd<double, Eigen::Upper, Eigen::RowMajor> SpSymMatProd;
typedef Spectra::SparseCholesky<double, Eigen::Upper> SpCholesky;
typedef Spectra::SparseRegularInverse<double, Eigen::Upper, Eigen::RowMajor> SpRegularInverse;


template < typename Scalar,
    int SelectionRule,
    typename OpType,
    typename BOpType,
    int GEigsMode >
    SymGEigsSolver<Scalar, SelectionRule, OpType, BOpType, GEigsMode>& CreateSolverEngine
    (const SpMat& A, const SpMat&  B, int selectRule, int gMode,  int nMode, int ncv)
{
    SpSymMatProd op(A);

    if (gMode == Spectra::GEIGS_CHOLESKY)
    {
        SpCholesky  Bop(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpCholesky, GEigsMode>
            ges1(op, Bop, nMode, ncv);
        return ges1;
    }
    if (gMode == Spectra::GEIGS_REGULAR_INVERSE)
    {
        SpRegularInverse Bop2(B);
        SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpRegularInverse, GEigsMode>
            ges(op, Bop2, nMode, ncv);
        return ges;
    }

    throw std::out_of_range("out of range for "+gMode);


}

I got compilation errors such as:

Error   C2664   'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>::SymGEigsSolver(OpType *,BOpType *,int,int)'
: cannot convert parameter 1
from 'SpSymMatProd' to 'SpSymMatProd *' 

And

C2440   'return' : cannot convert from
'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>'
to
'Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>&'   

And

C2664   Spectra::SymGEigsSolver<Scalar,SelectionRule,OpType,BOpType,GEigsMode>::SymGEigsSolver(OpType *,BOpType *,int,int)'
: cannot convert parameter 1 from 'SpSymMatProd' to 'SpSymMatProd *'

Solution

  • I figure out the answer by myself-- I mistype & at the method name. This is the correct solution:

    typedef SparseMatrix<double, Eigen::RowMajor> SpMat;
    typedef Spectra::SparseSymMatProd<double, Eigen::Upper, Eigen::RowMajor> SpSymMatProd;
    typedef Spectra::SparseCholesky<double, Eigen::Upper> SpCholesky;
    typedef Spectra::SparseRegularInverse<double, Eigen::Upper, Eigen::RowMajor> SpRegularInverse;
    
    
    template < typename Scalar,
        int SelectionRule,
        typename OpType,
        typename BOpType,
        int GEigsMode >
        SymGEigsSolver<Scalar, SelectionRule, OpType, BOpType, GEigsMode> CreateSolverEngine
        (const SpMat& A, const SpMat&  B, int selectRule, int gMode,  int nMode, int ncv)
    {
        SpSymMatProd op(A);
    
        if (gMode == Spectra::GEIGS_CHOLESKY)
        {
            SpCholesky  Bop(B);
            SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpCholesky, GEigsMode>
                ges1(op, Bop, nMode, ncv);
            return ges1;
        }
        if (gMode == Spectra::GEIGS_REGULAR_INVERSE)
        {
            SpRegularInverse Bop2(B);
            SymGEigsSolver<Scalar, SelectionRule, SpSymMatProd, SpRegularInverse, GEigsMode>
                ges(op, Bop2, nMode, ncv);
            return ges;
        }
    
        throw std::out_of_range("out of range for "+gMode);
    
    
    }