gpt4 book ai didi

c++ - 动态二维数组的运算符重载(加法)

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:31 28 4
gpt4 key购买 nike

所以我最近几个小时一直在解决一个问题,似乎无法阻止我的程序崩溃。问题是创建一个程序,该程序采用任意大小的矩阵,并且能够使用运算符重载将一个矩阵加到另一个矩阵上。当我尝试添加我类(class)的两个对象时,我的程序崩溃了。任何帮助是极大的赞赏!对于大量代码,我深表歉意,但我认为你们可能需要查看我的构造函数/值处理,我对这里的这个问题有点不知所措,所以这是我最后的选择。

类:

#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;
}

最佳答案

在任何时候您都不会初始化成员 int **matrixPointer1,您只是在创建一个本地成员。改变这个:

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

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

matrixPointer1 = new int*[row];

您也没有在 operator= 中进行任何重新分配,它要求两个 matrixType 具有相同的 row col 计数,这是你想要的吗?此外,setValue 将泄漏之前分配的内存。

关于c++ - 动态二维数组的运算符重载(加法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com