正确的模板声明和实施

Proper template declaration and implementation

本文关键字:声明      更新时间:2023-10-16

我正在尝试声明用于在C 中实现二维矩阵的模板声明。我以前从未使用过模板,并被要求使用它们。我只需要在正确的语法上进行声明的帮助,因为朋友的功能和模板的过载是令人困惑的,至少与我用过的问题相比,我的问题都令人困惑。

内部文档像往常一样无用。我最专注于弄清超载运营商的错误,老实说,它使我感到困惑至少一整天。

class Matrix
{
      public:
      Matrix(int sizeX, int sizeY, T initValue = T());

      T &operator()(int x, int y);
     template <class Type>
     friend ostream &operator<<(ostream &out, const Matrix<type> &m);
      template <class Mtype>
     friend Matrix<Mtype> operator+(const Matrix<MType> &m1, const Matrix<Mtype>& m2);
    private:
   vector< vector<T> > data;
   int dx, dy;
}
#ifndef MATRIX_CPP
Template <class T>
Matrix<T>::Matrix(int sizeX, int sizeY, T initValue){
dx = sizeX;
dy = sizeY;
initvalue = T(sizeX, sizeY);
}
T& operator()(int x, int y){
return T[x][y];
}

错误TypeName'T'不命名类型

'matrix :: matrix(int,t('

的重新定义无效

您缺少模板类型t。

template <class T>
 class Matrix
{
public:
     Matrix(int sizeX, int sizeY, T initValue = T());
...

请注意,模板代码实现应在标题文件中,而不是在CPP中。