【问题标题】:How to return the same template class reusing the template parameters in different order?如何以不同的顺序重用模板参数返回相同的模板类?
【发布时间】:2016-05-29 15:54:16
【问题描述】:

我有这门课:

template <class A_Type,int sizeA,int sizeB>
class Matrix {
...
...
...

Matrix transpose()const{
        Matrix<A_Type, sizeB, sizeA> tmp(this->_arrayofarrays[0][0]);
        for (int i=0;i<sizeA;i++)
            for (int j=0;j<sizeB;j++)
                tmp[i][j]=this->_arrayofarrays[j][i];
        return tmp;
    };
}

在这里可以看到,我只想得到一个大小为 B*A 的新矩阵

但我收到此错误:

Matrix.h:94:13: error: could not convert ‘tmp’ from ‘Matrix<int, 2, 3>’ to ‘Matrix<int, 3, 2>’

有什么想法吗?

【问题讨论】:

  • 你尝试Matrix&lt;A_Type, sizeB, sizeA&gt; transpose() { ...}了吗?

标签: c++ class templates


【解决方案1】:

确实没有将Matrix&lt;int,2,3&gt; 隐式转换为Matrix&lt;int,3,2&gt;。这些是不同的类型。

要修复它,请使用transpose() 显式声明返回类型:

 Matrix<A_Type, sizeB, sizeA> transpose() {
    // ^^^^^^^^^^^^^^^^^^^^^^ 
     // ...
 }

【讨论】:

    【解决方案2】:

    将函数transpose的输出类型更改为Matrix&lt;A_Type, sizeB, sizeA&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多