模板类之前得到预期的主表达式

问题描述 投票:0回答:0

我正在学习使用模板类

我创建了一个名为 Matrix 的类

template <class V, class I = int>
class Matrix{
    private:
        I m_rowstart;
        I m_columnstart;
        size_t nr , nc;
        vector<vector<V> > Mat;
    public:
        Matrix(size_t r, size_t c);
        I MaxRowIndex() const;
};

类定义

template <class V, class I>
Matrix<V, I>::Matrix(size_t r, size_t c){
    this->nr =r;
    this->nc =c;
    this->Mat =  vector<vector<V>>(r,vector<V>(c,0.0));
};

template <class V, class I>
I Matrix<V, I>::MaxRowIndex() const{
    return this->nr;
};

我试着调用主函数

Matrix<double, int> m1(10,10);
int r = m1.template MaxRowIndex<double, int> () ;
Compile error
D:\project_CPP\C++_study\instrument_pricing\main.cpp:13:25: error: 'Matrix<V, I>::MaxRowIndex() const [with V = double; I = int]' is not a template [-fpermissive]
   13 |     int r = m1.template MaxRowIndex<double, int> () ;
      |                         ^~~~~~~~~~~
D:\project_CPP\C++_study\instrument_pricing\main.cpp:13:37: error: expected primary-expression before 'double'
   13 |     int r = m1.template MaxRowIndex<double, int> () ;
      |                                     ^~~~~~
c++ templates g++
© www.soinside.com 2019 - 2024. All rights reserved.