运算符重载+动态矩阵
Operator overloading + dynamic matrix
所以我决定重新启动我的程序,除了 + 运算符现在可以正常工作。我收到读取访问冲突异常
头文件
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
class MatrixType
{
private:
int **matrix;
int row;
int col;
public:
void setElement(int r, int c, int newvalue);
void display();
const MatrixType& operator=(const MatrixType& mat);
MatrixType operator+(const MatrixType& mat) const;
friend std::ostream & operator << (std::ostream & out, const MatrixType & mat);
MatrixType(int r, int c);
MatrixType(string fileName);
MatrixType(const MatrixType& mat);
MatrixType();
~MatrixType();
};
执行文件
#include "matrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void MatrixType::setElement(int r, int c, int newvalue)
{
matrix[r][c] = newvalue;
}
void MatrixType::display()
{
for (int i = 0; i < row; i++)
{
for (int r = 0; r < col; r++)
cout << matrix[i][r] << " ";
cout << endl;
}
cout << endl;
}
const MatrixType& MatrixType::operator=(const MatrixType& mat)
{
if (row != mat.row)
{
cout << "The matrixes are not identical" << endl;
return *this;
}
for (int i = 0; i < row; i++)
{
for (int r = 0; r < col; r++)
{
matrix[i][r] = mat.matrix[i][r];
}
}
return *this;
}
MatrixType MatrixType::operator+(const MatrixType& mat) const
{
MatrixType tempMatrix;
if (row != mat.row)
{
cout << "The matrixes are not identical can not be added together" << endl;
return *this;
}
else
{
for (int i = 0; i < mat.row; i++)
{
for (int r = 0; r < mat.col; r++)
{
tempMatrix.matrix[i][r] = mat.matrix[i][r] + matrix[i][r];
}
}
return tempMatrix;
}
}
std::ostream & operator << (std::ostream & out, const MatrixType & mat)
{
for (int i = 0; i < mat.row; i++) {
for (int j = 0; j < mat.col; j++) {
out << mat.matrix[i][j] << " ";
}
out << std::endl;
}
return out;
}
MatrixType::MatrixType(int r, int c)
{
matrix = new int*[r];
for (int i = 0; i < r; i++)
{
matrix[i] = new int[c];
}
row = r;
col = c;
for (int i = 0; i < row; i++)
{
for (int s = 0; s < col; s++)
{
matrix[i][s] = 0;
}
}
}
MatrixType::MatrixType(string fileName)
{
int r;
int c;
int z;
ifstream myFile;
myFile.open(fileName);
myFile >> r;
myFile >> c;
matrix = new int*[r];
for (int i = 0; i < r; i++)
{
matrix[i] = new int[c];
}
for (int i = 0; i < r; i++)
{
for (int s = 0; s < c; s++)
{
myFile >> z;
matrix[i][s] = z;
}
}
row = r;
col = c;
myFile.close();
}
MatrixType::MatrixType(const MatrixType& mat)
{
row = mat.row;
col = mat.col;
matrix = new int*[row];
for (int i = 0; i < row; i++)
{
matrix[i] = new int[col];
}
for (int i = 0; i < row; i++)
{
for (int s = 0; s < col; s++)
{
matrix[i][s] = mat.matrix[i][s];
}
}
}
MatrixType::MatrixType()
{
}
MatrixType::~MatrixType()
{
for (int i = 0; i < row; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
源代码
#include "MatrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
MatrixType M1("matrixfile1.txt");
MatrixType M2("matrixfile2.txt");
MatrixType M3("matrixfile3.txt");
cout << "Matrix 1:" << endl << M1 << endl << endl;
cout << "Matrix 2:" << endl << M2 << endl << endl;
cout << "Matrix 3:" << endl << M3 << endl << endl;
MatrixType M4 = M1 + M3;
return 0;
}
所以基本上目标是检查矩阵是否相同,否则你应该只看到矩阵大小不同的打印。
看起来像
的matrixfile1,2,3
3 3
1 1 1
1 1 1
1 1 1
显示数组及其元素的维度。我的问题是我可以做些什么来让我的运算符函数更好地工作,因为它目前还不能将矩阵加在一起。尽管其他一切再次起作用,但它只是获得读取访问冲突的操作员。
MatrixType::MatrixType()
{
}
这根本不会初始化成员 row
、col
或 matrix
,因此尝试使用其中任何成员的当前值是未定义的行为。
由于您根本没有提供更改 MatrixType
尺寸的方法,即使使用 operator=
,似乎也没有什么好的使用默认构造函数的方法, MatrixType()
默认构造函数可能根本不应该存在。
MatrixType MatrixType::operator+(const MatrixType& mat) const
{
MatrixType tempMatrix;
您的 operator+
首先使用默认构造函数初始化 tempMatrix
。所以 tempMatrix
的维度是...?
所以我决定重新启动我的程序,除了 + 运算符现在可以正常工作。我收到读取访问冲突异常
头文件
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
class MatrixType
{
private:
int **matrix;
int row;
int col;
public:
void setElement(int r, int c, int newvalue);
void display();
const MatrixType& operator=(const MatrixType& mat);
MatrixType operator+(const MatrixType& mat) const;
friend std::ostream & operator << (std::ostream & out, const MatrixType & mat);
MatrixType(int r, int c);
MatrixType(string fileName);
MatrixType(const MatrixType& mat);
MatrixType();
~MatrixType();
};
执行文件
#include "matrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void MatrixType::setElement(int r, int c, int newvalue)
{
matrix[r][c] = newvalue;
}
void MatrixType::display()
{
for (int i = 0; i < row; i++)
{
for (int r = 0; r < col; r++)
cout << matrix[i][r] << " ";
cout << endl;
}
cout << endl;
}
const MatrixType& MatrixType::operator=(const MatrixType& mat)
{
if (row != mat.row)
{
cout << "The matrixes are not identical" << endl;
return *this;
}
for (int i = 0; i < row; i++)
{
for (int r = 0; r < col; r++)
{
matrix[i][r] = mat.matrix[i][r];
}
}
return *this;
}
MatrixType MatrixType::operator+(const MatrixType& mat) const
{
MatrixType tempMatrix;
if (row != mat.row)
{
cout << "The matrixes are not identical can not be added together" << endl;
return *this;
}
else
{
for (int i = 0; i < mat.row; i++)
{
for (int r = 0; r < mat.col; r++)
{
tempMatrix.matrix[i][r] = mat.matrix[i][r] + matrix[i][r];
}
}
return tempMatrix;
}
}
std::ostream & operator << (std::ostream & out, const MatrixType & mat)
{
for (int i = 0; i < mat.row; i++) {
for (int j = 0; j < mat.col; j++) {
out << mat.matrix[i][j] << " ";
}
out << std::endl;
}
return out;
}
MatrixType::MatrixType(int r, int c)
{
matrix = new int*[r];
for (int i = 0; i < r; i++)
{
matrix[i] = new int[c];
}
row = r;
col = c;
for (int i = 0; i < row; i++)
{
for (int s = 0; s < col; s++)
{
matrix[i][s] = 0;
}
}
}
MatrixType::MatrixType(string fileName)
{
int r;
int c;
int z;
ifstream myFile;
myFile.open(fileName);
myFile >> r;
myFile >> c;
matrix = new int*[r];
for (int i = 0; i < r; i++)
{
matrix[i] = new int[c];
}
for (int i = 0; i < r; i++)
{
for (int s = 0; s < c; s++)
{
myFile >> z;
matrix[i][s] = z;
}
}
row = r;
col = c;
myFile.close();
}
MatrixType::MatrixType(const MatrixType& mat)
{
row = mat.row;
col = mat.col;
matrix = new int*[row];
for (int i = 0; i < row; i++)
{
matrix[i] = new int[col];
}
for (int i = 0; i < row; i++)
{
for (int s = 0; s < col; s++)
{
matrix[i][s] = mat.matrix[i][s];
}
}
}
MatrixType::MatrixType()
{
}
MatrixType::~MatrixType()
{
for (int i = 0; i < row; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
源代码
#include "MatrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
MatrixType M1("matrixfile1.txt");
MatrixType M2("matrixfile2.txt");
MatrixType M3("matrixfile3.txt");
cout << "Matrix 1:" << endl << M1 << endl << endl;
cout << "Matrix 2:" << endl << M2 << endl << endl;
cout << "Matrix 3:" << endl << M3 << endl << endl;
MatrixType M4 = M1 + M3;
return 0;
}
所以基本上目标是检查矩阵是否相同,否则你应该只看到矩阵大小不同的打印。 看起来像
的matrixfile1,2,33 3
1 1 1
1 1 1
1 1 1
显示数组及其元素的维度。我的问题是我可以做些什么来让我的运算符函数更好地工作,因为它目前还不能将矩阵加在一起。尽管其他一切再次起作用,但它只是获得读取访问冲突的操作员。
MatrixType::MatrixType()
{
}
这根本不会初始化成员 row
、col
或 matrix
,因此尝试使用其中任何成员的当前值是未定义的行为。
由于您根本没有提供更改 MatrixType
尺寸的方法,即使使用 operator=
,似乎也没有什么好的使用默认构造函数的方法, MatrixType()
默认构造函数可能根本不应该存在。
MatrixType MatrixType::operator+(const MatrixType& mat) const
{
MatrixType tempMatrix;
您的 operator+
首先使用默认构造函数初始化 tempMatrix
。所以 tempMatrix
的维度是...?