gpt4 book ai didi

c++ - 如何在 Qt 中为矩阵类定义 [ ][ ] 运算符?

转载 作者:行者123 更新时间:2023-11-30 04:44:52 25 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




我正在 Qt 中开发自己的矩阵类。我知道有一个 QGenericMatrix 类模板,但我需要在运行时设置大小,这在这个模板中是不可能的。除此之外,我认为这是一个很好的项目,可以通过实现它来恢复我的线性代数知识。

但是,我已经能够如下定义 * 运算符(乘法):

我的矩阵.h

public:
MyMatrix(int rows, int cols, double initValues=0.0); // constructor to create NxM matrix with N=col, M=rows

MyMatrix& operator*(double value); // multiply matrix with a double
private:
int rows;
int cols;
double **mat;
void initMatrix(int rows=1, int cols=1); // initialise the matrix, if no rows,cols are given it creates a "1x1 matrix"

我的矩阵.cpp

// constructor
MyMatrix::MyMatrix(int rows, int cols, double initValues)
{
initMatrix(rows, cols);
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
this->mat[i][j] = initValues;
}
}
}
// multiply each element in matrix by value
MyMatrix& MyMatrix::operator*(double value) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
this->mat[i][j] = this->mat[i][j] * value;
}
}
return *this;
}
// initialise all matrix cells
void MyMatrix::initMatrix(int rows, int cols)
{
this->rows = rows; // assign argument to member variable rows
this->cols = cols; // assign argument to member variable cols

this->mat = new double*[this->rows]; // initialise mat with list of doubles ptrs with length rows
for (int i = 0; i < this->rows; ++i) { // iterate over each row-element
this->mat[i] = new double[this->cols]; // initialise each rows-element with list of doubles with length cols
}
}

主文件

    int rows = 2;
int cols = 3;
MyMatrix mat1(rows, cols, 1.0); // creates matrix with all elements 1.0

mat1 = mat1 * 3.0;

注意我只提取了相关部分,类已经增长,所以我想完全发布所有三个文件会更加困惑。

到现在为止还挺好。以上似乎做了它应该做的。

现在,我希望能够直接访问矩阵中的每个元素。类似于如何访问 QVector 中的元素,如下所示:

读取一个元素:

   double temp = mat1[2][2]               // read the element in row=2, column=2

写入一个元素:

   double temp = 17;
mat1[2][2] = temp // set value of element in row=2, column=2 to given double temp (here 17).

但我不知道如何定义这个 [][] 运算符。我尝试了以下类似于双倍乘法的定义,因为我需要给出行和列。我想我尝试:

我的矩阵.h

   MyMatrix& operator[int c][int r](double value);    // write
MyMatrix& operator[int c][int r](); // read

我想到的覆盖/读取行 r 和列 c 中的元素的实现应该如下所示:

我的矩阵.cpp

// write to element
MyMatrix& MyMatrix::operator[int r][int c](double value) {
this->mat[r][c] = value;
return *this;
}
// read to element
double& MyMatrix::operator[int r][int c]() {
return this->mat[r][c];
}

但这并不能解决问题。

顺便说一句:甚至在编译 QtCreator 之前说:
/path/MyMatrixClass/mymatrix.cpp:60: error: expected ']'
/path/MyMatrixClass/mymatrix.cpp:60: to match this '['
/path/MyMatrixClass/mymatrix.cpp:60: error: expected '(' for function-style cast or type construction
/path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'r'
/path/MyMatrixClass/mymatrix.cpp:61: error: use of undeclared identifier 'c'

我已经尝试搜索这些错误,但到目前为止,我找不到任何可以让我知道我想要实现什么的线索。

所以,也许有人可以给我一个链接,告诉我在哪里可以找到一个例子或一些关于如何实现我想要的建议的建议。

PS:稍后我也希望能够提取某行或某列,但我想(=希望)一旦我知道如何以正确的方式处理 [][] 运算符,这应该是直截了当的。

这是我第一次真正为一个类定义自己的运算符。我想我从 * 运算符那里得到了大致的想法。 (我也有 + 和 - 运营商已经工作)。然而,到目前为止,我主要将 Qt 用于 GUI 构建、使用 QVectors 进行简单数据处理、绘制光谱等。所以,我想我只是缺少一些基本的 Qt/c++ 语法。

最佳答案

您可以让矩阵使用 operator()使用尽可能多的参数。方括号不起作用。

double const& operator()(size_t rowIndex, size_t colIndex) const;
double& operator()(size_t rowIndex, size_t colIndex);

然后,您可以像这样使用矩阵类
Matrix M(n, n);
for (size_t i = 0; i < n; ++i) {
M(i, i) = 1.0;
}
return M;

关于c++ - 如何在 Qt 中为矩阵类定义 [ ][ ] 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57529791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com