矩阵运算符不起作用+超载

发布于 2025-01-02 09:33:40 字数 1577 浏览 2 评论 0原文

我正在尝试制作一个示例(只是示例!我知道它会泄漏)应用程序来学习 C++ 中的运算符重载,但我得到总和元素的零值......我怀疑问题在于 int复制构造函数。

具体实现如下:

    class Matrix{
    public:
        Matrix(int row, int col);
        Matrix(const Matrix& src);
        float& set(int row, int col);
        float get(int row, int col);
        const Matrix & operator+(const Matrix& rhs);

    private:
        float* data;
        int nrow;
        int ncol;
};

Matrix::Matrix(int row, int col){
    nrow = row;
    ncol = ncol;
    data = new float[nrow*ncol];
}

Matrix::Matrix(const Matrix& src){
    nrow = src.nrow;
    ncol = src.ncol;
    data = new float[nrow*ncol];

    for(int i = 0; i < nrow*ncol; i++){
        data[i] = src.data[i];
    }
}

float& Matrix::set(int row, int col){
    return data[row*ncol+col];
}

float Matrix::get(int row, int col){
    return data[row*ncol+col];
}

const Matrix & Matrix::operator+(const Matrix& rhs){

    if (this->nrow == rhs.nrow && this->ncol == rhs.ncol){
        Matrix* m = new Matrix(rhs.nrow, rhs.ncol);
        for(int i=0; i< nrow*ncol; i++){
            m->data[i] = data[i] + rhs.data[i];
        }
        return *m;
    } else {
        throw -1;
    }
}

#include <iostream>

using namespace std;

int main ()
{
    Matrix A(1,1);
    Matrix B(1,1);

    A.set(0,0)=1;   
    B.set(0,0)=2;

    cout << A.get(0,0) << endl;
    cout << B.get(0,0) << endl;

    Matrix C = A + B; // Marix C(A+B);
    cout << C.get(0,0) << endl;

    return 0;
}

I'm trying to make a example (just example! I know it leaks) app to learn operator overloading in C++, but I get the value of zero to the elements of the sum.... I suspect that the problem is int the copy constructor.

The Detailed implementation is below:

    class Matrix{
    public:
        Matrix(int row, int col);
        Matrix(const Matrix& src);
        float& set(int row, int col);
        float get(int row, int col);
        const Matrix & operator+(const Matrix& rhs);

    private:
        float* data;
        int nrow;
        int ncol;
};

Matrix::Matrix(int row, int col){
    nrow = row;
    ncol = ncol;
    data = new float[nrow*ncol];
}

Matrix::Matrix(const Matrix& src){
    nrow = src.nrow;
    ncol = src.ncol;
    data = new float[nrow*ncol];

    for(int i = 0; i < nrow*ncol; i++){
        data[i] = src.data[i];
    }
}

float& Matrix::set(int row, int col){
    return data[row*ncol+col];
}

float Matrix::get(int row, int col){
    return data[row*ncol+col];
}

const Matrix & Matrix::operator+(const Matrix& rhs){

    if (this->nrow == rhs.nrow && this->ncol == rhs.ncol){
        Matrix* m = new Matrix(rhs.nrow, rhs.ncol);
        for(int i=0; i< nrow*ncol; i++){
            m->data[i] = data[i] + rhs.data[i];
        }
        return *m;
    } else {
        throw -1;
    }
}

#include <iostream>

using namespace std;

int main ()
{
    Matrix A(1,1);
    Matrix B(1,1);

    A.set(0,0)=1;   
    B.set(0,0)=2;

    cout << A.get(0,0) << endl;
    cout << B.get(0,0) << endl;

    Matrix C = A + B; // Marix C(A+B);
    cout << C.get(0,0) << endl;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2025-01-09 09:33:40
Matrix::Matrix(int row, int col){
    nrow = row;
    ncol = ncol;
    data = new float[nrow*ncol];
}

其中存在一个拼写错误,导致您的代码具有未定义的行为。

修复它:(

    ncol = col;

确保将编译器警告/诊断级别调到最大,GCC 会捕获此问题。)

您也泄漏了 float[],因此您的代码不完整。不要忘记添加适当的析构函数,并始终遵循三原则

Matrix::Matrix(int row, int col){
    nrow = row;
    ncol = ncol;
    data = new float[nrow*ncol];
}

There's a typo in there that results in your code having undefined behavior.

Fix it with:

    ncol = col;

(Make sure you turn you compilers warning/diagnostics level to the maximum, GCC catches this.)

You're leaking your float[]s too, so your code isn't complete. Don't forget to add proper destructors, and always follow the rule of three.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文