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



我正在使用Spectra C++库,它建立在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);

}

相关内容

  • 没有找到相关文章

最新更新