运算符动态2D数组的重载(加法)

时间:2015-12-08 08:57:01

标签: c++

所以我在过去的几个小时里一直在解决问题,似乎无法阻止我的程序崩溃。问题是创建一个程序,它采用任何大小的矩阵,并能够使用运算符重载将一个添加到另一个。当我尝试添加我班级的两个对象时,我的程序正在崩溃。任何帮助是极大的赞赏!我为大量的代码道歉,但我想你们可能需要看看我的构造函数/值处理,我在这里遇到这个问题有点不知所以这是我的最后一招。

类别:

#ifndef matrixType_H
#define matrixType_H

class matrixType
{
    friend std::ostream& operator<<(std::ostream& osObject, const matrixType& cObject);

public:
    void setValue();
    matrixType operator+(const matrixType& object)const;
    matrixType operator-(const matrixType& object)const;
    matrixType operator*(const matrixType& object)const;
    const matrixType& operator=(const matrixType& object);
    matrixType();
    matrixType(const matrixType& object);
    matrixType(int, int);
    ~matrixType();

private:
    int row;
    int col;
    int **matrixPointer1;
};

#endif

实现:

#include <iostream>
#include "matrixType.h"
using namespace std;

void matrixType::setValue()
{
    int **matrixPointer1 = new int*[row];
    for (int i = 0; i < row; i++)
        matrixPointer1[i] = new int[col];

    cout << "Enter each element of the matrix: " << endl;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            cin >> matrixPointer1[i][j];
        }
    }
}

matrixType matrixType::operator+(const matrixType& object)const
{
    matrixType temp(row, col);

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            temp.matrixPointer1[i][j] = matrixPointer1[i][j] + object.matrixPointer1[i][j];
        }
    } 

    return temp;
}

matrixType::matrixType(const matrixType& object)
{
    row = object.row;
    col = object.col;

    int **matrixPointer1 = new int*[row];
    for (int i = 0; i < row; i++)
        matrixPointer1[i] = new int[col];

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            matrixPointer1[i][j] = object.matrixPointer1[i][j];
        }
    }

}

const matrixType& matrixType::operator=(const matrixType& object)
{
    row = object.row;
    col = object.col;

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            matrixPointer1[i][j] = object.matrixPointer1[i][j];
        }
    }

    return *this;
}

ostream& operator<<(ostream& osObject, const matrixType& cObject)
{
    for (int i = 0; i < cObject.row; i++){
        for (int j = 0; j < cObject.col; j++){
            osObject << cObject.matrixPointer1[i][j] << " ";
        }
        cout << endl;
    }

    return osObject;
}

matrixType::matrixType(int r, int c)
{
    row = r;
    col = c;

    int **matrixPointer1 = new int*[row];
    for (int i = 0; i < row; i++)
        matrixPointer1[i] = new int[col];

    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++){
            matrixPointer1[i][j] = 0;
        }
    }
}

matrixType::~matrixType()
{
    for (int i = 0; i < row; i++){
        delete[]matrixPointer1[i];
    }

    delete[]matrixPointer1;
}

部分来源:

cout << "Enter the number of rows in matrix one: ";
cin >> rows1;

cout << "Enter the number of columns in matrix one: ";
cin >> cols1;

cout << "Enter the number of rows in matrix two: ";
cin >> rows2;

cout << "Enter the number of columns in matrix two: ";
cin >> cols2;

matrixType matrix1(rows1, cols1);
matrixType matrix2(rows2, cols2);
matrixType matrix3(rows1, cols1);

matrix1.setValue();
matrix2.setValue();

cout << "Which operation would you like to perform? (+, -, *): ";
cin >> operation;

if (operation == "+"){
    if (rows1 == rows2 && cols1 == cols2){
        matrix3 = matrix1 + matrix2; //crashes when the + operator function is called
        cout << matrix3; //The << operator also causes a crash when tried by itself
    }

    else
        cout << "Error: Matrix sizes are not equal." << endl;
}

1 个答案:

答案 0 :(得分:0)

您无法初始化会员int **matrixPointer1,而只是创建一个本地会员。改变这个:

int **matrixPointer1 = new int*[row];

到此(在构造函数中,复制构造函数,setValue):

matrixPointer1 = new int*[row];

您还没有在operator=中进行任何重新分配,并且要求两个matrixType具有相同的rowcol计数,这就是您的意思想?此外,setValue将泄漏先前分配的内存。