在 C++ 中构造模板函数时出现编译错误 C2664 和 C2440

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

我正在使用 Spectra C++ library, which is built on top of the Eigen C++ 库。

我需要根据方法中给定的 SelectionRuleGeigsMode 参数构造一个正确的 SymGEigsSolver 对象。 SymGEigsSolver的定义如下:

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

然而,当我尝试编译以下函数时:

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);


}

我遇到编译错误,例如:

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

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

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

我自己想出了答案 -- 我在方法名称处打错了 &。这是正确的解决方案:

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);


}