From 70e545a4ef17f08d2b966c7eb3dd98f7433b351a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Tue, 15 Mar 2022 17:09:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=93=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/inc/matrix.hpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp index 4243d8a..6409a6a 100644 --- a/SoftwareDesign/Code/inc/matrix.hpp +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -2,7 +2,7 @@ * @Author: iR * @Date: 2022-03-11 16:44:46 * @LastEditors: iR - * @LastEditTime: 2022-03-12 17:25:28 + * @LastEditTime: 2022-03-15 17:02:42 * @FilePath: \Code\inc\matrix.hpp * @Description: * @@ -17,7 +17,15 @@ #include "../inc/vector.hpp" template -class Matrix +class MatBase +{ // 定义基类,其中包含了矩阵元素读取的纯虚函数 +public: + virtual T &at(int y, int x) = 0; // 定义纯虚函数 + virtual const T &at(int y, int x) const = 0; // 定义纯虚函数 +}; + +template +class Matrix : public MatBase { private: int _height; @@ -91,7 +99,7 @@ public: /** * @description: =运算符重载 */ - T operator=(const Matrix &b); + void operator=(const Matrix &b); /** * @description: +=运算符重载 @@ -100,6 +108,13 @@ public: * @return None */ void operator+=(const Matrix &b); + void operator+=(T &a) + { + for (int i = 0; i < _width * _height;i++) + { + _mat[i] = a; + } + } /** * @description: +=运算符重载 @@ -236,7 +251,7 @@ Matrix::Matrix(const Matrix &another) } template -T Matrix::operator=(const Matrix &another) +void Matrix::operator=(const Matrix &another) { try { @@ -245,6 +260,7 @@ T Matrix::operator=(const Matrix &another) _mat = new Vector; for (int i = 0; i < _width * _height; i++) _mat->put(i, another[i]); + } catch (const char *msg) { @@ -278,12 +294,6 @@ void Matrix::operator+=(const Matrix &b) } } -/** - * @description: +=运算符重载 - * @note:即 c = a + b,其中*this即为a,返回值即为c - * @param {*} - * @return None - */ template Matrix Matrix::operator+(const Matrix &b) const {