鱼C论坛

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

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

[复制链接]
发表于 2021-9-8 15:22:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhangjunxian 于 2021-9-8 15:24 编辑

今天看书看到了 关于 class templete 的内容,然后有习题 然后 我在做习题的时候出现了以下问题
1、Matrix 中 友元函数 的使用 在 vscode 里面 会报警告 这个 friend declaration 'Matrix<element>& operator*(Matrix<element>&, Matrix<element>&)' declares a non-template function [-Wnon-template-friend]
2、当 无参数的Matrix 的构造函数 中 row 和 column 的值 为 10的时候 执行程序 会自动终止程序 ,但是调试的时候又没有,能执行完毕。还有  值如果小一点 比如 都是 5 5的时候程序不会出错。
3、为什么传入参数的时候不能直接传入 数组 就是 以 {1,2,3,4,5} 这样的形式传入会说没有 相应的构造函数
  1. #include <iostream>

  2. using namespace std;

  3. template <typename element>
  4. class Matrix{
  5.     friend Matrix<element>& operator * (Matrix<element> &x1,Matrix<element> &x2);  //就是这两条都会报警告
  6.     friend Matrix<element>& operator + (Matrix<element> &x1,Matrix<element> &x2);
  7.     private:
  8.         int row;
  9.         int column;
  10.         element **arr;
  11.     public:
  12.         Matrix(){
  13.             row = 10; column = 10;             //这个都为10的时候执行程序会出错 自动终止退出
  14.             arr = new element* [row];
  15.             for(int i=0;i<row;i++){
  16.                 arr[i] = new element(column);
  17.                 for(int j=0;j<column;j++){
  18.                     if(i==j) arr[i][j] = 1;
  19.                     else arr[i][j] = 0;
  20.                 }
  21.             }
  22.         }
  23.         Matrix(element a[],int r, int c){
  24.             row = r;
  25.             column = c;
  26.             arr = new element* [row];
  27.             int cout = 0;
  28.             for(int i=0;i<row;i++){
  29.                 arr[i] = new element(column);
  30.                 for(int j=0;j<row;j++){
  31.                    arr[i][j] = a[cout];
  32.                    cout++;
  33.                 }
  34.             }
  35.         }
  36.         ~Matrix(){for(int i = 0;i<row;i++){
  37.             delete [] arr[i];
  38.         }}
  39.         int &operator() (int row ,int column) {return arr[row][column];};
  40.         int &operator() (int row, int column) const {return arr[row][column];};
  41.         void print(){
  42.             for(int i=0;i<row;i++){
  43.                 cout << "[ ";
  44.                 for(int j=0;j<column;j++){
  45.                     cout << arr[i][j] << " ";
  46.                 }
  47.                 cout <<"]"<<endl;
  48.             }
  49.             cout << endl;
  50.         };
  51. };

  52. template <typename element>
  53. Matrix<element>& operator * (Matrix<element> &x1,Matrix<element> &x2){
  54.     Matrix<element>* result = new Matrix<element>();
  55.     for(int i=0;i<4;i++){
  56.         int r = 0;
  57.         for(int j=0;j<4;j++){
  58.             for(int k=0;k<4;k++){
  59.                 r += x1(k,j)*x2(i,k);
  60.             }
  61.             result->arr[i][j] = r;
  62.         }
  63.     }
  64.     return *result;
  65. }

  66. template <typename element>
  67. Matrix<element>& operator + (Matrix<element> &x1,Matrix<element> &x2){
  68.     Matrix<element>* result = new Matrix<element>();
  69.     for(int i=0;i<4;i++){
  70.         for(int j=0;j<4;j++){
  71.             result->arr[i][j] = x1(i,j)+x2(i,j);
  72.         }
  73.     }
  74.     return *result;
  75. }

  76. int main(){
  77.     Matrix<float> x = Matrix<float>();
  78.     x.print();
  79.     float w[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4};
  80.     //Matrix<float> y = Matrix<float>({1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4},4,4);  //就是这个 不能以这个形式传入
  81.     Matrix<float> y = Matrix<float>(w,4,4);
  82.     y.print();
  83.   
  84. }
复制代码
最佳答案
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
回复

使用道具 举报

发表于 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
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-13 13:49:38 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 18:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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