动态矩阵类
Dynamic matrix class
本文关键字:动态 更新时间:2023-10-16
我需要构造一个矩阵类,并用一个特定的函数给它赋值。
下面是我的代码:
class MATRIX
{
int row, col;
double **p;
public:
MATRIX(int, int);
void Set(int, int, double);
~MATRIX();
};
MATRIX::MATRIX(int x, int y)
{
row = x;
col = y;
p = new double*[x];
for (int i = 0; i < x; i++)
{
p[i] = new double[y];
}
}
void MATRIX::Set(int a, int b, double d)
{
p[a][b] = d;
}
MATRIX::~MATRIX()
{
for (int i = 0; i < row; i++)
delete[] p[i];
delete[] p;
}
int main()
{
MATRIX A(2, 3); // Initializes a matrix with size 2x3
MATRIX B(7, 4); // Initializes a matrix with size 7x4
A.Set(1, 2, 4.7); // Sets the value of A[1][2] to 4.7
B.Set(0, 3, 2.9); // Sets the value of B[0][3] to 2.9
}
在Set
函数中,我在调试器中看到这个:
this->p was 0x1110112
我该如何修复它?
因为这是c++,这看起来更像C。也许一个好的第一步是编辑一些代码。我建议您将c风格的数组更改为std::数组或std::vector。头文件应该是这样的:
template<typename T, std::size_t rows, std::size_t columns>
class MATRIX
{
std::array<std::array<T, columns>, rows> matrix_data;
public:
MATRIX();
void Set(int, int, double);
~MATRIX();
};
这使用了一个模板类,这意味着你必须在主
中做这样的操作MATRIX<double, 2, 3> A;
我没有时间写完整的代码,但我认为这将使你开始!古德勒克!
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 输出没有重复元素的动态数组(收缩数组)C++
- C++为线程工作动态地分割例程
- 正在插入动态数组
- 在c++中使用动态分配的问题
- C++中的动态对象与非动态对象
- 如何在动态数组上使用搜索函数
- 视觉studo 2019中的漫画和静态/动态绑定
- 从C++中的数字输入动态创建矩阵
- 如何从QToolBox中动态创建的QLineEdit中获取文本
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁