代码之家  ›  专栏  ›  技术社区  ›  Graviton

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

  •  -1
  • Graviton  · 技术社区  · 6 年前

    我正在使用 Spectra C++ 库,它构建在 Eigen C++ 图书馆

    我需要构造一个正确的 SymGEigsSolver 对象基于给定的 SelectionRule GeigsMode 方法中的参数。定义 符号解算器 具体如下:

    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 *'
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Graviton    6 年前

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

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