這是我關于 Matrix 的代碼(我決定練習 OOP 撰寫自己的 Matrix 類)
矩陣.hpp
#ifndef MATRIX_HEADER
#define MATRIX_HEADER
typedef unsigned int u_int;
class Matrix
{
double **mtrx;
u_int x, y;
public:
Matrix(u_int a, u_int b);
Matrix(const Matrix &);
~Matrix();
double det();
Matrix operator (const Matrix &) const;
Matrix operator-(const Matrix &) const;
Matrix operator*(const Matrix &) const;
friend Matrix operator*(const Matrix &, const double &);
Matrix operator/(const Matrix &) const;
double *operator[](const u_int idx) const { return mtrx[idx]; }
bool IsEqual(const Matrix &) const;
u_int GetX() const { return x; }
u_int GetY() const { return y; }
};
#endif
矩陣.cpp
#include "Matrix.hpp"
Matrix::Matrix(u_int a, u_int b) : x(a), y(b)
{
mtrx = new double *[x];
for (u_int i = 0; i < x; i )
mtrx[i] = new double[y];
}
Matrix::Matrix(const Matrix &ref)
{
if (mtrx)
{
for (u_int i = 0; i < x; i )
delete[] mtrx[i];
delete[] mtrx;
}
x = ref.x;
y = ref.y;
*mtrx = new double[x];
for (u_int i = 0; i < x; i )
mtrx[i] = new double[y];
}
Matrix::~Matrix()
{
if (mtrx)
{
for (u_int i = 0; i < x; i )
delete[] mtrx[i];
delete[] mtrx;
}
}
bool Matrix::IsEqual(const Matrix &a) const // If sizes of matrixes are equal
{ // matrixes are equal
return (a.GetX() == x) && (a.GetY() == y);
}
Matrix Matrix::operator (const Matrix &a) const
{
if (!IsEqual(a)) // Check on equality matrixes
return Matrix(1,1); // I have not any idea yet what
Matrix matrix(x, y); // should it give is sizes
for (u_int i = 0; i < x; i ) // of Matrix are not equal
for (u_int j = 0; j < y; j )
matrix[i][j] = a.mtrx[i][j] mtrx[i][j];
return matrix;
}
主檔案
#include <stdio.h>
#include "Matrix.hpp"
int main()
{
Matrix a(2, 5);
Matrix b(2, 5);
for (u_int i = 0; i < a.GetX(); i )
{
for (u_int j = 0; j < a.GetY(); j )
{
a[i][j] = i j;
}
}
for (u_int i = 0; i < b.GetX(); i )
{
for (u_int j = 0; j < b.GetY(); j )
{
b[i][j] = i j;
}
}
Matrix c = a b;
return 0;
}
當我打開我的程式時,添加兩個矩陣后它會引發分段錯誤。在這種情況下,對我來說更有趣的是Matrix.cpp, Matrix Matrix::operation ....
當我洗掉這 2 行(檢查相等 2 矩陣)時,當我打開我的程式時,它不會給我一個分段錯誤,但是當我添加這 2 行時,程式給我一個 sf 你能告訴我為什么它是這樣作業的嗎?
uj5u.com熱心網友回復:
您可能會考慮使用一維陣列而不是double[x*y]二維陣列。它將使記憶體管理更容易一些,因為您將只有 1 個陣列要處理,而不是多個陣列。double*[x]double[y]
在任何情況下,您的Matrix(const Matrix &)復制建構式都不應該包含delete[]任何內容mtrx,因為mtrx尚未初始化(它指向隨機記憶體)。并且該建構式的其余部分也沒有正確分配陣列。您在建構式中正確分配了陣列Matrix(u_int, u_int),因此只需將該邏輯復制到復制建構式中。double然后通過實際復制正在復制的值來完成復制建構式Matrix。
至于operator ,如果您不知道應該return為不同大小的矩陣做什么,那么我建議您throw改為例外。
此外,即使您當前的代碼還沒有使用它,您也應該添加一個復制分配operator=來完成規則 3(您已經有一個復制建構式和一個解構式)。
試試這個:
#ifndef MATRIX_HEADER
#define MATRIX_HEADER
typedef unsigned int u_int;
class Matrix
{
double **mtrx;
//or: double *mtrx;
u_int x, y;
public:
Matrix(u_int a, u_int b);
Matrix(const Matrix &);
~Matrix();
double det();
Matrix& operator=(const Matrix &);
Matrix operator (const Matrix &) const;
Matrix operator-(const Matrix &) const;
Matrix operator*(const Matrix &) const;
friend Matrix operator*(const Matrix &, const double &);
Matrix operator/(const Matrix &) const;
double* operator[](const u_int idx) const {
return mtrx[idx];
//or: return &mtrx[idx*x];
}
bool IsEqualSize(const Matrix &) const;
u_int GetX() const { return x; }
u_int GetY() const { return y; }
};
#endif
#include "Matrix.hpp"
#include <utility>
#include <stdexcept>
Matrix::Matrix(u_int a, u_int b)
: x(a), y(b)
{
mtrx = new double*[x];
for (u_int i = 0; i < x; i) {
mtrx[i] = new double[y];
}
// or: mtrx = new double[x*y];
}
Matrix::Matrix(const Matrix &ref)
: x(ref.x), y(ref.y)
{
mtrx = new double*[x];
for (u_int i = 0; i < x; i) {
mtrx[i] = new double[y];
for (u_int j = 0; j < y; j) {
mtrx[i][j] = ref.mtrx[i][j];
}
}
/* or:
u_int size = x*y;
mtrx = new double[size];
for (u_int i = 0; i < size; i) {
mtrx[i] = ref.mtrx[i];
}
*/
}
Matrix::~Matrix()
{
// this loop is not needed for a 1D array...
for (u_int i = 0; i < x; i) {
delete[] mtrx[i];
}
delete[] mtrx;
}
bool Matrix::IsEqualSize(const Matrix &a) const
{
return (a.GetX() == x) && (a.GetY() == y);
}
Matrix& Matrix::operator=(const Matrix &a)
{
if (&a != this)
{
Matrix tmp(a);
std::swap(mtrx, tmp.mtrx);
}
return *this;
}
Matrix Matrix::operator (const Matrix &a) const
{
if (!IsEqualSize(a))
throw std::logic_error("Cannot add matrices of different sizes");
Matrix matrix(x, y);
for (u_int i = 0; i < x; i) {
for (u_int j = 0; j < y; j)
matrix.mtrx[i][j] = mtrx[i][j] a.mtrx[i][j];
}
/* or:
u_int size = x*y;
for (u_int i = 0; i < size; i) {
matrix.mtrx[i] = mtrx[i] a.mtrx[i];
}
*/
return matrix;
}
...
#include <stdio.h>
#include "Matrix.hpp"
int main()
{
Matrix a(2, 5);
Matrix b(2, 5);
for (u_int i = 0; i < a.GetX(); i)
{
for (u_int j = 0; j < a.GetY(); j)
{
a[i][j] = i j;
}
}
for (u_int i = 0; i < b.GetX(); i)
{
for (u_int j = 0; j < b.GetY(); j)
{
b[i][j] = i j;
}
}
Matrix c = a b;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471700.html
上一篇:可運行介面實體化
