【发布时间】:2020-11-28 01:34:06
【问题描述】:
我为矩阵类实现了加法和乘法运算符的重载。我不明白错误在哪里,或者更确切地说,我猜操作符的声明中有错误,但我不知道如何具体修复它。 有人可以推荐一本关于面向对象编程的好书。我觉得我显然缺乏信息
#include <iostream>
using namespace std;
template <typename T>
class MATRIX
{
private:
T** M;
int m;
int n;
public:
MATRIX()
{
n = m = 0;
M = nullptr;
}
MATRIX(int _m, int _n)
{
m = _m;
n = _n;
M = (T**) new T * [m];
for (int i = 0; i < m; i++)
M[i] = (T*)new T[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
M[i][j] = 0;
}
MATRIX(const MATRIX& _M)
{
m = _M.m;
n = _M.n;
M = (T**) new T * [m];
for (int i = 0; i < m; i++)
M[i] = (T*) new T[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
M[i][j] = _M.M[i][j];
}
T GetMij(int i, int j)
{
if ((m > 0) && (n > 0))
return M[i][j];
else
return 0;
}
void SetMij(int i, int j, T value)
{
if ((i < 0) || (i >= m))
return;
if ((j < 0) || (j >= n))
return;
M[i][j] = value;
}
void Print(const char* ObjName)
{
cout << "Object: " << ObjName << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
cout << M[i][j] << "\t";
cout << endl;
}
cout << "---------------------" << endl << endl;
}
MATRIX operator=(const MATRIX& _M)
{
if (n > 0)
{
for (int i = 0; i < m; i++)
delete[] M[i];
}
if (m > 0)
{
delete[] M;
}
m = _M.m;
n = _M.n;
M = (T**) new T * [m];
for (int i = 0; i < m; i++)
M[i] = (T*) new T[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
M[i][j] = _M.M[i][j];
return *this;
}
MATRIX operator+(const MATRIX& _M) {
MATRIX M6(_M.n, _M.m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
M6.M[i][j] = _M.M[i][j]+this-> M[i][j];
}
return M6;
}
MATRIX operator*(const MATRIX& _M) {
MATRIX M5(_M.n, _M.m);
for (int i = 0; i < _M.n; i++)
{
for (int j = 0; j < _M.m; j++)
{
for (int k = 0; k < m; k++)
{
M5.M[i][j] += M[i][k]*_M.M[k][j];
}
}
}
return M5;
}
bool operator ==(const MATRIX& _M) {
return this->n == _M.n && this->m == _M.m;
}
bool operator !=(const MATRIX& _M) {
return !(this->n == _M.n && this->m == _M.m);
}
~MATRIX()
{
if (n > 0)
{
for (int i = 0; i < m; i++)
delete[] M[i];
}
if (m > 0)
delete[] M;
}
};
void main()
{
MATRIX<int> M(2, 3);
M.Print("M");
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
M.SetMij(i, j, i + j);
M.Print("M");
MATRIX<int> M2 = M;
M2.Print("M2");
MATRIX<int> M3;
M3 = M;
M3.Print("M3");
MATRIX<int> M4;
M4 = M3 = M2 = M;
M4.Print("M4");
////////////////////////
bool result = M == M3;// ==
if (result == 1) {
cout << "true";
}
else if (result == 0) {
cout << "false";
}
cout << endl << "---------------------" << endl << endl;
//////////////////////////
bool result1 = M != M3;// !=
if (result1 == 1) {
cout << "true";
}
else if (result1 == 0) {
cout << "false";
}
cout << endl << "---------------------" << endl << endl;
MATRIX<int> M1;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
M1.SetMij(i, j, i + j);
M1.Print("M1");
M.Print("M");
MATRIX<int> M6 = M + M1;
M6.Print("M");
MATRIX<int> M5 = M * M1;
M5.Print("M");
}```
thanks in advance
[enter image description here][1]
[1]: https://i.stack.imgur.com/TjEOp.png
【问题讨论】:
-
抱歉,请教书籍等站外资源的建议是题外话。
-
这段代码在做什么表明有错误?拒绝编译?编译但产生不正确的输出? “某处出现错误”不是很有帮助。
-
与代码中的错误无关,我建议您尝试使用更具描述性的变量名称。
m和M都是MATRIX类的成员变量,具有非常不同的含义,这一事实有点令人困惑,因为名为M#的变量数量众多,用于各种数字。 -
想必您还希望
operator=按引用返回,而不是按值返回。就数学而言,您在operator*中返回的矩阵的维度是不正确的。如果你有一个m x n矩阵并且你将它乘以一个n x u矩阵,那么乘积将是m x u,而不是n x u。您甚至不检查以确保操作数具有兼容的尺寸。 -
您的赋值运算符有很大缺陷。使用这个:
MATRIX& operator=(const MATRIX& _M) { MATRIX temp(_M); std::swap(temp.m, m); std::swap(temp.n, n); std::swap(temp.M, M); return *this; }。然后当你让它工作时,将你的成员变量的名称更改为比 1 字母名称更有意义。
标签: c++ oop matrix operator-overloading