鱼C论坛

 找回密码
 立即注册
查看: 1167|回复: 2

[已解决]关于c++ class templete的问题

[复制链接]
发表于 2021-9-8 17:57:41 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>

  2. using namespace std;

  3. template <typename element> class Matrix {
  4.     template <typename _element>
  5.     friend Matrix<_element> &operator*(Matrix<_element> &x1,
  6.               Matrix<_element> &x2); //就是这两条都会报警告
  7.     template <typename _element>
  8.     friend Matrix<_element> &operator+(Matrix<_element> &x1, Matrix<_element> &x2);

  9.   private:
  10.     int row;
  11.     int column;
  12.     element **arr;

  13.   public:
  14.     Matrix() {
  15.         row = 10;
  16.         column = 10; //这个都为10的时候执行程序会出错 自动终止退出
  17.         arr = new element *[row];
  18.         for(int i = 0; i < row; i++) {
  19.             //arr[i] = new element(column);
  20.             arr[i] = new element[column];
  21.             for(int j = 0; j < column; j++) {
  22.                 if(i == j)
  23.                     arr[i][j] = 1;
  24.                 else
  25.                     arr[i][j] = 0;
  26.             }
  27.         }
  28.     }
  29.     Matrix(const std::initializer_list<element> &rhs, int r, int c) {
  30.         row = r; column = c;
  31.         arr = new element *[row];
  32.         for(int i = 0; i < row; ++i) {
  33.             arr[i] = new element[column];
  34.             for(int j = 0; j < column; ++j) {
  35.                 //arr[i][j] = rhs[i * column + j];      // 奇怪,std::initializer_list 没有 operator[]
  36.                 arr[i][j] = *(rhs.begin() + (i * column + j));
  37.             }
  38.         }
  39.     }
  40.     Matrix(element a[], int r, int c) {
  41.         row = r;
  42.         column = c;
  43.         arr = new element *[row];
  44.         int cout = 0;
  45.         for(int i = 0; i < row; i++) {
  46.             //arr[i] = new element(column);
  47.             arr[i] = new element[column];
  48.             for(int j = 0; j < row; j++) {
  49.                 arr[i][j] = a[cout];
  50.                 cout++;
  51.             }
  52.         }
  53.     }
  54.     ~Matrix() {
  55.         for(int i = 0; i < row; i++) {
  56.             delete[] arr[i];
  57.         }
  58.         delete[] arr;
  59.     }
  60.     //int &operator()(int row, int column) { return arr[row][column]; };
  61.     int &operator()(int row, int column) const { return arr[row][column]; };
  62.     void print() {
  63.         for(int i = 0; i < row; i++) {
  64.             cout << "[ ";
  65.             for(int j = 0; j < column; j++) {
  66.                 cout << arr[i][j] << " ";
  67.             }
  68.             cout << "]" << endl;
  69.         }
  70.         cout << endl;
  71.     };
  72. };

  73. template <typename element>
  74. Matrix<element> &operator*(Matrix<element> &x1, Matrix<element> &x2) {
  75.     Matrix<element> *result = new Matrix<element>();
  76.     for(int i = 0; i < 4; i++) {
  77.         int r = 0;
  78.         for(int j = 0; j < 4; j++) {
  79.             for(int k = 0; k < 4; k++) {
  80.                 r += x1(k, j) * x2(i, k);
  81.             }
  82.             result->arr[i][j] = r;
  83.         }
  84.     }
  85.     return *result;
  86. }

  87. template <typename element>
  88. Matrix<element> &operator+(Matrix<element> &x1, Matrix<element> &x2) {
  89.     Matrix<element> *result = new Matrix<element>();
  90.     for(int i = 0; i < 4; i++) {
  91.         for(int j = 0; j < 4; j++) {
  92.             result->arr[i][j] = x1(i, j) + x2(i, j);
  93.         }
  94.     }
  95.     return *result;
  96. }

  97. int main() {
  98.     Matrix<float> x = Matrix<float>();
  99.     x.print();
  100.     float w[] = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
  101.      Matrix<float> z = Matrix<float>({1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4},4,4);
  102.     // //就是这个 不能以这个形式传入
  103.     Matrix<float> y = Matrix<float>(w, 4, 4);
  104.     y.print();
  105. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-18 02:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表