我有一个小问题...我了解什么是EXC_BAD_ACCESS错误,而且我通常知道如何解决该错误,但这完全使我陷入了困境。我在一个类中拥有所有这些,这里是一个方法:
double Matrix::get_element(int r, int c) const {
//Retrieve the element at row r and column c
//Should not modify the value stored in Matrix but return a double copy of the value
double currentValue = matrix[r][c];
return currentValue;
}
现在,我的另一段代码调用了此方法:
std::string Matrix::to_string() const {
std::string result;
double current;
Matrix working = *this;
std::ostringstream oss;
oss << "[";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
current = 0.0;
current = working.get_element(i, j);
oss << " " << current << " ";
}
oss << "; ";
}
oss << "]";
result = oss.str();
return result;
}
我知道在调用
working.get_element(i, j);
时,该工作对象具有3行和3个列。变量列表向我显示了get_element()
方法之前的行和列都设置为3。在该方法中,我可以在get_element(0, 0)
处获取值,但不能在get_element(0, 1)
处获取值。我不明白为什么会这样...有人知道为什么还是需要我更多的代码才能理解为什么调用这些方法?
编辑:
这是头文件:
class Matrix {
private:
//Any variables required
int rows;
int cols;
double **matrix;
public:
Matrix(); //Working M
~Matrix(); //Working M
Matrix(int r, int c); //Working M
int getRows();
int getCols();
void set_element(int r, int c, double val); //Working M
double get_element(int r, int c) const; //Working M
void clear(); //Working M
bool is_empty(); //Working M
bool is_identity(); //Working M
const Matrix transpose(); //Working M
int minorMat(double **dest, const int row, const int col, int order); //Working M
double get_determinent(); //Working M
double higherDeterminents(int order); //Working M
const Matrix operator+(const Matrix &rhs); //Working M
const Matrix operator-(const Matrix &rhs); //Working M
const Matrix operator*(const Matrix &rhs);
bool operator==(const Matrix &rhs); //NOT assessed
const Matrix operator*(const double &rhs);
const Matrix operator/(const double &rhs);
Matrix & operator=(const Matrix &rhs);
std::string to_string() const;
};
不要忽略对不起的评论。这是构造函数/析构函数:
Matrix::Matrix() {
//Basic Constructor
rows = 1;
cols = 1;
matrix = new double*[rows];
for (int i = 0; i < rows; ++i) {
matrix[i] = new double[cols];
}
}
Matrix::~Matrix() {
//Basic Deconstructor
for (int i = 0; i < rows; ++i) {
delete[] matrix[i];
}
delete[] matrix;
rows = NULL;
cols = NULL;
matrix = NULL;
}
Matrix::Matrix(int r, int c) {
//Empty matrix (all 0's) with r rows and c columns, if they are -ve, set to 1
rows = r;
cols = c;
if (cols < 0)
cols = 1;
if (rows < 0)
rows = 1;
matrix = NULL;
matrix = new double*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new double[cols];
}
}
编辑2:
Matrix & Matrix::operator=(const Matrix &rhs) {
//rhs is matrix to be copied
//rhs compied into Matrix called on
double toCopy;
for (int i = 0; i < rhs.rows; i++) {
for (int j = 0; j < rhs.cols; j++) {
toCopy = rhs.get_element(i, j);
this->set_element(i, j, toCopy);
}
}
return *this;
}
最佳答案
当您不声明如何声明和初始化matrix
元素时,我们无法说出。在您的CTOR中使用类似的方法应该可以:
class Matrix {
float matrix[3][3];
...
}
不要忘记在CTOR中将其初始化为有意义的东西。
顺便说一句:为什么要这样做:
Matrix working = *this;
?您可以简单地使用this->get_element(i, j);
代替,它不会调用整个对象的复制。 [1]
编辑:,因为您更新了答案,所以进行了更新。您应该小心复制CTOR和
operator=()
语句。很容易进行两次删除或类似的丑陋操作。编辑2:我认为问题是此行:
Matrix working = *this;
您正在创建
working
对象的新副本this
。但是working
仅初始化为1列和1行(在标准CTOR中定义)。我不确定在调用set_element
或get_element
时是否正在检查边界,所以我猜您正在覆盖数组的边界。我认为最好的方法是删除
Matrix working = *this;
行并坚持上面的提示:this->get_element(i, j);
中的std::string Matrix::to_string() const
。