C++ 矩阵类算子&x2B;最后,以失败告终
基本上我必须做一个矩阵类实现。我做了大部分,但我偶然发现了一个问题,我真的不知道到底是什么错了 首先,这是我的C++ 矩阵类算子&x2B;最后,以失败告终,c++,segmentation-fault,operator-overloading,C++,Segmentation Fault,Operator Overloading,基本上我必须做一个矩阵类实现。我做了大部分,但我偶然发现了一个问题,我真的不知道到底是什么错了 首先,这是我的矩阵。hpp标题: #include <iostream> #include <vector> class Matrix { int columns; int rows; std::vector<std::vector<int>> vals; int matcheck (std::vector<std::vec
矩阵。hpp
标题:
#include <iostream>
#include <vector>
class Matrix {
int columns;
int rows;
std::vector<std::vector<int>> vals;
int matcheck (std::vector<std::vector<int>>);
public:
Matrix (int, int, std::vector<std::vector<int>>);
Matrix (int, int);
~Matrix();
const Matrix operator + (const Matrix&) const;
const Matrix operator * (const Matrix&) const;
Matrix &operator = (const Matrix&);
void inverse (Matrix&);
void setValues (Matrix&, std::vector<std::vector<int>>);
void printMatrix ();
static void changeSize (Matrix&, int, int);
static void transpose (Matrix&);
};
现在,上一个操作之前的所有操作都运行得很好。但是当我到达最后一个(mat5=mat4+mat2
)时,我得到了segfault。另外,如果我使用*
来代替,我仍然会得到一个segfault。如果我尝试实例化一个矩阵对象,或者如果我使用任何函数,比如transpose()
,也会发生同样的情况
我修改了操作符的实现,还用谷歌搜索了一下,它们看起来还不错,或者至少在我看来是这样
这可能只是我的愚蠢,但我真的无法理解。提前谢谢
编辑:
函数转置()
:
编辑:
我还共享了整个
matrix.cpp
源文件,因此我可以提供一个完全可复制的程序。我放了一个pastebin链接,因为我已经有很多代码了,在这里粘贴一个完整的源文件会使文章变得巨大 changeSize
更新行
和列
成员,但将向量保留为空。之后对它的任何引用都将是未定义的行为
changeSize
和transpose
可以/应该是非静态成员函数,因为您将矩阵作为第一个参数传递,并在同一个对象中返回结果。加法的维度检查错误–a*b=b*a,但是,除非a=b,否则不能添加这些。请发布转置
。运算符+
中的大小测试不正确。有许多价值观的组合,它们的产品是相同的。正确的测试是<代码>行= = Mat.Real&&Cube=Mat。列。考虑将类A模板作为两个维度作为模板参数。这样添加两个大小不同的矩阵将是一个编译时错误。尽管如此,我还是无法真正理解为什么它只是将我在最后一个mat5.printMatrix()
之后放置的任何内容进行分段,除了重载运算符=
。还有,为什么就在这里,我在一开始就使用操作符+
和*
,它们可以工作,但由于某种原因,在最后一行中,它只会给我segfault。@CatalinC访问向量超出边界是未定义的行为。就C++语言而言,字面上任何事情都可能发生,包括恶魔从你的鼻子里飞出来,但也包括你的代码出现在某个时间。@阿特格尔,哦,我不是说我不知道为什么它不能用向量来工作。我的意思是,我修复了向量的问题,但我仍然得到了segfault,这就是为什么我感到困惑的原因。您是否尝试过通过调试器运行代码?如果没有,这里有一个很好的阅读:@CatalinC,因为您的代码不可编译(因为它缺少矩阵
构造函数),我们只能猜测。这个问题包括一个问题。
const Matrix Matrix::operator + (const Matrix& mat) const {
if ( !(rows == mat.rows && columns == mat.columns) ) {
std::cout << "For addition the matrices must be the same size.\n";
exit(-1);
}
Matrix res (mat.rows, mat.columns);
for (unsigned int row = 0; row < rows; ++row) {
for (unsigned int col = 0; col < columns; ++col) {
res.vals[row][col] += this->vals[row][col] + mat.vals[row][col];
}
}
return res;
}
const Matrix Matrix::operator * (const Matrix& mat) const {
Matrix res (rows, mat.columns);
if (columns != mat.rows) {
std::cout<<"For multiplication the matrix A's columns must be the same number as matrix B's rows\n";
exit(-1);
}
for (unsigned int row = 0; row < rows; ++row) {
for (unsigned int col = 0; col < mat.columns; ++col) {
int sum = 0;
for (unsigned int k = 0; k < columns; ++k) {
sum = sum + (vals[row][k] * mat.vals[k][col]);
}
res.vals[row][col] = sum;
}
}
return res;
}
Matrix &Matrix::operator = (const Matrix& mat) {
rows = mat.rows;
columns = mat.columns;
vals = mat.vals;
return *this;
}
#include <iostream>
#include "matrix.hpp"
int main() {
Matrix mat(4, 4, { {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1} });
Matrix mat2(4, 4, { {2, 2, 2, 2}, {2, 2, 2, 2}, {2, 2, 2, 2}, {2, 2, 2, 2} });
Matrix mat5(4, 2, { {1, 12}, {5, 66}, {9, 6}, {7, 19}});
Matrix mat3 = mat + mat2;
mat3.printMatrix();
Matrix mat4 = mat * mat5;
mat4.printMatrix();
mat4 = mat2;
mat4.printMatrix();
Matrix MAT(4, 4);
MAT.printMatrix();
MAT = mat5;
mat5.printMatrix();
Matrix::transpose(mat5);
mat5.printMatrix();
mat5 = mat;
mat5.printMatrix();
mat5 = mat4 + mat2;
return 0;
}
void Matrix::transpose (Matrix& mat) {
Matrix res (mat.rows, mat.columns);
res = mat;
changeSize (mat, mat.columns, mat.rows);
for (unsigned int col = 0; col < res.rows; ++col) {
for (unsigned int row = 0; row < res.columns; ++row) {
mat.vals[row][col] = res.vals[col][row];
}
}
}
void Matrix::changeSize (Matrix& mat, int rows, int cols) {
mat.vals.clear();
mat.rows = rows;
mat.columns = cols;
}