错误:内存位置的 std::length_error [英] Error : std::length_error at memory location
本文介绍了错误:内存位置的 std::length_error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我调用转置函数时,出现此错误:错误:
When I call transpose function I got this error: Error :
std::length_error at memory location
我不知道到底是什么导致了错误
I do not know exactly what could cause the error
//class Matrix.h
class Matrix
{
private:
std::vector<std::vector<double>> content;
size_t numberOfRows;
size_t numberOfColumns;
double determinant;
};
inline Matrix::Matrix(size_t nRows, size_t nColumns)
: content(nRows,std::vector<double>(nColumns)),
numberOfRows(nRows), numberOfColumns(nColumns) {}
const double getValue(size_t i, size_t j) const {
return (i >= numberOfRows) || (j >= numberOfColumns) ? NULL : content[i][j];
}
void setValue(size_t i, size_t j, double value) {
(i >= numberOfRows) || (j >= numberOfColumns) ? content[i][j] = NULL : content[i][j] = value;
determinant = NULL;
}
Matrix Matrix::transpose()
{
Matrix result(numberOfColumns, numberOfRows);
for (size_t i = 0; i < numberOfRows; i++)
for (size_t j = 0; j < numberOfColumns; j++)
result.setValue(j, i, getValue(i, j));
return result; /// Error here
}
推荐答案
解决方案是将对象作为指针返回或将变量作为引用传递,正确的例子如下:
The solution is return the object as a pointer or pass a variable as reference, here is the correct example:
Matrix* Matrix::transpose()
{
Matrix *result=new Matrix(numberOfColumns, numberOfRows);
for (size_t i = 0; i < numberOfRows; i++)
for (size_t j = 0; j < numberOfColumns; j++)
result.setValue(j, i, getValue(i, j));
return result;
}
谢谢
这篇关于错误:内存位置的 std::length_error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文