OOP C++ 运算符中的问题

pex*_*a12 1 c++ oop

我对 C++ 很陌生。我的问题是写一个关于矩阵的 OOP C++ 程序(创建一个矩阵类并向这个类添加一些方法)这是我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

class Matrix 
{
    private:
        int col, row;
        double *a;
    public:
        Matrix(int col = 1, int row = 1) {
            this->col = col; this->row = row;
        }
        ~Matrix() {
            delete a;
            col = row = 0;
        }
        void insertMatrix() {
            a = new double[this->col * this->row];
            for (int i = 0; i < this->row; i++)
                for (int j = 0; j < this->col; j++) {
                    cout << endl << "a[" << i + 1 << "][" << j + 1 << "] = ";
                    cin >> this->a[i * this->col + j];
                }
        }
        void printMatrix() {
            for (int i = 0; i < this->row; i++) {
                for (int j = 0; j < this->col; j++)
                    cout << setw(9) << this->a[i * this->col + j];
                cout << endl;
            }
            cout << endl;
        }
        int getCol() {
            return col;
        }
        int getRow() {
            return row;
        }
        Matrix operator+(Matrix);
        Matrix operator-(Matrix);
};

Matrix Matrix::operator+(Matrix x) {
    if (x.col != col || x.row != row) {
        cout << endl << "Can't add these two matrices";
        exit(0);
    }
    Matrix sum(x.col, x.row);
    sum.a = new double(sum.col * sum.row);
    for (int i = 0; i < this->col * this->row; i++)
        sum.a[i] = a[i] + x.a[i];
    return sum;
}

Matrix Matrix::operator-(Matrix x) {
    if (x.col != this->col || x.row != this->row) {
        cout << endl << "Can't subtract these two matrices";
        exit(0);
    }
    Matrix dif(this->col, this->row);
    dif.a = new double(dif.col * dif.row);
    for (int i = 0; i < this->col * this->row; i++)
        dif.a[i] = this->a[i] - x.a[i];
    return dif;
}

int main()
{
    int row, col;
    cout << endl << "Column = "; cin >> col; cout << endl << "Row = "; cin >> row;
    Matrix A(col, row), B(col, row);
    A.insertMatrix(); B.insertMatrix();
    cout << "Matrix A: " << endl; A.printMatrix(); 
    cout << "Matrix B: " << endl; B.printMatrix();
    cout << "Matrix (A + B)" << endl; (A + B).printMatrix();
    cout << "Matrix (A - B)" << endl; (A - B).printMatrix();
}
Run Code Online (Sandbox Code Playgroud)

我根本看不出任何错误。我可以编译程序。但是每次我尝试输入一些数字时,程序都会冻结并显示“停止工作”消息并得到错误的答案。我在 Windows 8 中使用 Orwell Dev C++。谁能解释一下原因?

Pau*_*zie 5

一个错误是您使用new不正确。

 sum.a = new double(sum.col * sum.row);
Run Code Online (Sandbox Code Playgroud)

以上动态创建了一个双精度值,并将值初始化为sum.col * sum.row.

你应该使用new[]

 sum.a = new double[sum.col * sum.row];
Run Code Online (Sandbox Code Playgroud)

那么你必须使用delete[],而不是delete在使用时new[]

另一个错误是您没有a在构造函数中初始化指针。当 Matrix 被销毁时,析构函数将delete在指向 who-knows-where 的指针上发出 a 。

因此,这个简单的单行程序存在问题:

int main() {
   Matrix m;
}  // < -- problems here 
Run Code Online (Sandbox Code Playgroud)

解决方法是确保您的指针已初始化:

Matrix(int mcol = 1, int mrow = 1) : a(0), col(mcol), row(mrow) {}
Run Code Online (Sandbox Code Playgroud)

注意a现在初始化为0。还要注意成员初始化列表的用法。

但是,另一个错误是您的Matrix类缺少用户定义的复制构造函数和赋值运算符。当您Matrix在此处执行返回或按值传递时:

Matrix Matrix::operator+(Matrix x)
Run Code Online (Sandbox Code Playgroud)

副本只会对指针进行浅拷贝,因此您的程序将出现内存泄漏、双重释放错误等。请阅读“3 规则”:

三分法则是什么?

其他事宜:

不要exit(0)在类代码内调用。如果我想使用您的 Matrix 类,并且我给 operator + 一个大小不正确的 Matrix,请不要关闭我的应用程序!相反,抛出异常,或发出assert().

请参阅此处: exit() 在应返回引用的函数内调用