我是傻逼,我知道为啥了

This commit is contained in:
iridiumR 2022-03-12 17:26:10 +08:00
parent af7d67b808
commit 88f7d7107f
2 changed files with 45 additions and 44 deletions

View file

@ -2,7 +2,7 @@
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:44:46 * @Date: 2022-03-11 16:44:46
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 14:45:22 * @LastEditTime: 2022-03-12 17:25:28
* @FilePath: \Code\inc\matrix.hpp * @FilePath: \Code\inc\matrix.hpp
* @Description: * @Description:
* *
@ -28,7 +28,7 @@ protected:
public: public:
~Matrix() ~Matrix()
{ {
delete[] _mat; delete _mat;
} }
/** /**
* @description: , 2*2, 0 * @description: , 2*2, 0
@ -60,14 +60,14 @@ public:
*/ */
Matrix(int w, int h, const T elements[]); Matrix(int w, int h, const T elements[]);
/** // /**
* @description: 访 // * @description: 以顺序访问指针的值初始化矩阵元素
* @note: , // * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确
* @param {int} w // * @param {int} w
* @param {int} h // * @param {int} h
* @param {T*} data // * @param {T*} data
*/ // */
Matrix(int w, int h, T *data); // Matrix(int w, int h, T *data);
/** /**
* @description: * @description:
@ -117,7 +117,7 @@ public:
*/ */
T &at(int w, int h) T &at(int w, int h)
{ {
double &ref = _mat->get(h * _width + w); T &ref = _mat->get(h * _width + w);
return ref; return ref;
} }
@ -129,7 +129,7 @@ public:
*/ */
const T &at(int w, int h) const const T &at(int w, int h) const
{ {
double &ref = _mat->get(h * _width + w); T &ref = _mat->get(h * _width + w);
return ref; return ref;
} }
@ -186,36 +186,37 @@ Matrix<T>::Matrix(int w, int h, const T elements[])
} }
} }
template <class T> //这个方法似乎有问题
Matrix<T>::Matrix(int w, int h, T *data) // template <class T>
{ // Matrix<T>::Matrix(int w, int h, T *data)
int size = w * h; // {
try // int size = w * h;
{ // try
//实际上似乎并不能在这里判断数组是否合法 // {
// if (w * h > size) // //实际上似乎并不能在这里判断数组是否合法
// throw "Error: The number of array elements is less than the matrix elements.\n"; // // if (w * h > size)
// if (w * h < size) // // throw "Error: The number of array elements is less than the matrix elements.\n";
// throw "Error: The number of array elements is greater than the matrix elements.\n"; // // if (w * h < size)
if (data == NULL) // // throw "Error: The number of array elements is greater than the matrix elements.\n";
throw 1; // if (data == NULL)
_width = w; // throw 1;
_height = h; // _width = w;
T temp; // _height = h;
_mat = new Vector<T>; // _mat = new Vector<T>;
for (int i = 0; i < size; i++) // for (int i = 0; i < size; i++)
_mat->put(i, *(data+sizeof(temp)*i)); // _mat->put(i, *(data+i));
} // }
catch (const int a)
{ // catch (const int a)
if (a == 1) // {
{ // if (a == 1)
std::cout << "Error: Empty array. Set elements to 0." << std::endl; // {
for (int i = 0; i < size; i++) // std::cout << "Error: Empty array. Set elements to 0." << std::endl;
_mat->put(i, 0); // for (int i = 0; i < size; i++)
} // _mat->put(i, 0);
} // }
} // }
// }
template <class T> template <class T>
Matrix<T>::Matrix(const Matrix &another) Matrix<T>::Matrix(const Matrix &another)

View file

@ -2,7 +2,7 @@
* @Author: iR * @Author: iR
* @Date: 2022-03-11 16:47:31 * @Date: 2022-03-11 16:47:31
* @LastEditors: iR * @LastEditors: iR
* @LastEditTime: 2022-03-12 17:12:04 * @LastEditTime: 2022-03-12 17:25:39
* @FilePath: \Code\inc\vector.hpp * @FilePath: \Code\inc\vector.hpp
* @Description: * @Description:
* *
@ -40,7 +40,7 @@ public:
Vector(int len, T value); Vector(int len, T value);
//析构函数 //析构函数
// ~Vector() { delete[] _v; } ~Vector() { delete[] _v; }
//获取值 //获取值
T &get(int a); T &get(int a);