鱼C论坛

 找回密码
 立即注册
查看: 859|回复: 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} 这样的形式传入会说没有 相应的构造函数
#include <iostream>

using namespace std;

template <typename element>
class Matrix{
    friend Matrix<element>& operator * (Matrix<element> &x1,Matrix<element> &x2);  //就是这两条都会报警告
    friend Matrix<element>& operator + (Matrix<element> &x1,Matrix<element> &x2);
    private:
        int row;
        int column;
        element **arr;
    public:
        Matrix(){
            row = 10; column = 10;             //这个都为10的时候执行程序会出错 自动终止退出
            arr = new element* [row];
            for(int i=0;i<row;i++){
                arr[i] = new element(column);
                for(int j=0;j<column;j++){
                    if(i==j) arr[i][j] = 1;
                    else arr[i][j] = 0;
                }
            }
        }
        Matrix(element a[],int r, int c){
            row = r;
            column = c;
            arr = new element* [row];
            int cout = 0;
            for(int i=0;i<row;i++){
                arr[i] = new element(column);
                for(int j=0;j<row;j++){
                   arr[i][j] = a[cout];
                   cout++;
                }
            }
        }
        ~Matrix(){for(int i = 0;i<row;i++){
            delete [] arr[i];
        }}
        int &operator() (int row ,int column) {return arr[row][column];};
        int &operator() (int row, int column) const {return arr[row][column];}; 
        void print(){
            for(int i=0;i<row;i++){
                cout << "[ ";
                for(int j=0;j<column;j++){
                    cout << arr[i][j] << " ";
                }
                cout <<"]"<<endl;
            }
            cout << endl;
        };
};

template <typename element>
Matrix<element>& operator * (Matrix<element> &x1,Matrix<element> &x2){
    Matrix<element>* result = new Matrix<element>();
    for(int i=0;i<4;i++){
        int r = 0;
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                r += x1(k,j)*x2(i,k);
            }
            result->arr[i][j] = r;
        }
    }
    return *result;
}

template <typename element>
Matrix<element>& operator + (Matrix<element> &x1,Matrix<element> &x2){
    Matrix<element>* result = new Matrix<element>();
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            result->arr[i][j] = x1(i,j)+x2(i,j);
        }
    }
    return *result;
}

int main(){
    Matrix<float> x = Matrix<float>();
    x.print();
    float w[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4};
    //Matrix<float> y = Matrix<float>({1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4},4,4);  //就是这个 不能以这个形式传入
    Matrix<float> y = Matrix<float>(w,4,4);
    y.print();
  
}
最佳答案
2021-9-8 17:57:41
#include <iostream>

using namespace std;

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

  private:
    int row;
    int column;
    element **arr;

  public:
    Matrix() {
        row = 10;
        column = 10; //这个都为10的时候执行程序会出错 自动终止退出
        arr = new element *[row];
        for(int i = 0; i < row; i++) {
            //arr[i] = new element(column);
            arr[i] = new element[column];
            for(int j = 0; j < column; j++) {
                if(i == j)
                    arr[i][j] = 1;
                else
                    arr[i][j] = 0;
            }
        }
    }
    Matrix(const std::initializer_list<element> &rhs, int r, int c) {
        row = r; column = c;
        arr = new element *[row];
        for(int i = 0; i < row; ++i) {
            arr[i] = new element[column];
            for(int j = 0; j < column; ++j) {
                //arr[i][j] = rhs[i * column + j];      // 奇怪,std::initializer_list 没有 operator[]
                arr[i][j] = *(rhs.begin() + (i * column + j));
            }
        }
    }
    Matrix(element a[], int r, int c) {
        row = r;
        column = c;
        arr = new element *[row];
        int cout = 0;
        for(int i = 0; i < row; i++) {
            //arr[i] = new element(column);
            arr[i] = new element[column];
            for(int j = 0; j < row; j++) {
                arr[i][j] = a[cout];
                cout++;
            }
        }
    }
    ~Matrix() {
        for(int i = 0; i < row; i++) {
            delete[] arr[i];
        }
        delete[] arr;
    }
    //int &operator()(int row, int column) { return arr[row][column]; };
    int &operator()(int row, int column) const { return arr[row][column]; };
    void print() {
        for(int i = 0; i < row; i++) {
            cout << "[ ";
            for(int j = 0; j < column; j++) {
                cout << arr[i][j] << " ";
            }
            cout << "]" << endl;
        }
        cout << endl;
    };
};

template <typename element>
Matrix<element> &operator*(Matrix<element> &x1, Matrix<element> &x2) {
    Matrix<element> *result = new Matrix<element>();
    for(int i = 0; i < 4; i++) {
        int r = 0;
        for(int j = 0; j < 4; j++) {
            for(int k = 0; k < 4; k++) {
                r += x1(k, j) * x2(i, k);
            }
            result->arr[i][j] = r;
        }
    }
    return *result;
}

template <typename element>
Matrix<element> &operator+(Matrix<element> &x1, Matrix<element> &x2) {
    Matrix<element> *result = new Matrix<element>();
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            result->arr[i][j] = x1(i, j) + x2(i, j);
        }
    }
    return *result;
}

int main() {
    Matrix<float> x = Matrix<float>();
    x.print();
    float w[] = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
     Matrix<float> z = Matrix<float>({1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4},4,4);
    // //就是这个 不能以这个形式传入
    Matrix<float> y = Matrix<float>(w, 4, 4);
    y.print();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-8 17:57:41 | 显示全部楼层    本楼为最佳答案   
#include <iostream>

using namespace std;

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

  private:
    int row;
    int column;
    element **arr;

  public:
    Matrix() {
        row = 10;
        column = 10; //这个都为10的时候执行程序会出错 自动终止退出
        arr = new element *[row];
        for(int i = 0; i < row; i++) {
            //arr[i] = new element(column);
            arr[i] = new element[column];
            for(int j = 0; j < column; j++) {
                if(i == j)
                    arr[i][j] = 1;
                else
                    arr[i][j] = 0;
            }
        }
    }
    Matrix(const std::initializer_list<element> &rhs, int r, int c) {
        row = r; column = c;
        arr = new element *[row];
        for(int i = 0; i < row; ++i) {
            arr[i] = new element[column];
            for(int j = 0; j < column; ++j) {
                //arr[i][j] = rhs[i * column + j];      // 奇怪,std::initializer_list 没有 operator[]
                arr[i][j] = *(rhs.begin() + (i * column + j));
            }
        }
    }
    Matrix(element a[], int r, int c) {
        row = r;
        column = c;
        arr = new element *[row];
        int cout = 0;
        for(int i = 0; i < row; i++) {
            //arr[i] = new element(column);
            arr[i] = new element[column];
            for(int j = 0; j < row; j++) {
                arr[i][j] = a[cout];
                cout++;
            }
        }
    }
    ~Matrix() {
        for(int i = 0; i < row; i++) {
            delete[] arr[i];
        }
        delete[] arr;
    }
    //int &operator()(int row, int column) { return arr[row][column]; };
    int &operator()(int row, int column) const { return arr[row][column]; };
    void print() {
        for(int i = 0; i < row; i++) {
            cout << "[ ";
            for(int j = 0; j < column; j++) {
                cout << arr[i][j] << " ";
            }
            cout << "]" << endl;
        }
        cout << endl;
    };
};

template <typename element>
Matrix<element> &operator*(Matrix<element> &x1, Matrix<element> &x2) {
    Matrix<element> *result = new Matrix<element>();
    for(int i = 0; i < 4; i++) {
        int r = 0;
        for(int j = 0; j < 4; j++) {
            for(int k = 0; k < 4; k++) {
                r += x1(k, j) * x2(i, k);
            }
            result->arr[i][j] = r;
        }
    }
    return *result;
}

template <typename element>
Matrix<element> &operator+(Matrix<element> &x1, Matrix<element> &x2) {
    Matrix<element> *result = new Matrix<element>();
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            result->arr[i][j] = x1(i, j) + x2(i, j);
        }
    }
    return *result;
}

int main() {
    Matrix<float> x = Matrix<float>();
    x.print();
    float w[] = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
     Matrix<float> z = Matrix<float>({1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4},4,4);
    // //就是这个 不能以这个形式传入
    Matrix<float> y = Matrix<float>(w, 4, 4);
    y.print();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-13 13:49:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 07:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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