Присвоение массива полученному объекту
Вопрос
Я пытаюсь написать класс матрицы для проектов. Я отделил квадратную матрицу как полученный класс. Но когда я пытаюсь выполнить незначительную операцию для определителя, я не могу назначить участников матрицы на полученные объекты класса. Как я могу выполнить эту операцию? На самом деле, я много пробовал, я не смог добиться успеха. Вот код;
using namespace std;
class Matrix
{
friend ostream &operator<<(ostream &, const Matrix &);
friend istream &operator>>(istream &, const Matrix &);
private:
int num_col;
int num_row;
int **mits;
public:
Matrix(const int=0 ,const int=0);
Matrix (const Matrix &right);
~Matrix();
int DisplayMenu();
void gotoxy(int x, int y);
int getRow();
int getCol();
int** getMatrix();
virtual void print();
Matrix operator+(const Matrix&);
Matrix operator-(const Matrix&);
Matrix operator*(const Matrix&);
Matrix transMatrix(const Matrix&);
Matrix &operator=(const Matrix&);
};
#endif
# include <iostream>
# include "Matrix.h"
#include <windows.h>
# include <string>
using namespace std;
class SquareMatrix :public Matrix
{
int **sMits;
public:
SquareMatrix(const int=0 ,const int=0);
~SquareMatrix();
virtual void print();
SquareMatrix minor( const int,const int,const int);
int deter( SquareMatrix &mat);
/*SquareMatrix operator*(const SquareMatrix &);
virtual void print(const Matrix &);*/
};
SquareMatrix::SquareMatrix(const int row ,const int col)
:Matrix(row,col)
{
}
SquareMatrix::~SquareMatrix()
{
}
int SquareMatrix::deter(SquareMatrix &a)
{
SquareMatrix M;
int d = 0; // value of the determinant
int rows =a.getRow();
int cols =a.getCol();
sMits=a.getMatrix();
// this is a square matrix
if (rows == 1)
{
// this is a 1 x 1 matrix
d = sMits[0][0];
}
else if (rows == 2)
{
// this is a 2 x 2 matrix
// the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12
d = sMits[0][0]* sMits[1][1] - sMits[1][0]* sMits[0][1];
}
else
{
rows--;
cols--;
for (int c = 0; c <= cols; c++)
{
M= minor(c,rows,cols);
d += (c%2 + c%2 - 1) * sMits[0][c] * deter(M);
}
}
return d;
}
SquareMatrix SquareMatrix::minor(const int col,const int rows,const int cols)
{
//int rows =Matrix::getRow();
//int cols =Matrix::getCol();
int **Mits=Matrix::getMatrix();
SquareMatrix min(rows , cols);
cout<<min<<endl;
for (int r = 1; r <= rows ; r++)
{
for (int c = 0; c <= cols ; c++)
{
if (c!=(col))
{
if (c<col)
{
min.sMits[r-1][c] = Mits[r][c];
}
else if (c>col)
{
min.sMits[r-1][c-1] = Mits[r][c];
}
}
}
}
return min;
}
void SquareMatrix::print()
{
Matrix::print();
}
Решение
Много проблем с этим кодом, но я думаю, что особая проблема в том, что мин это SquareMatrix, и SquareMatrix Конструктор вызывает Матрица конструктор, который предположительно инициализирует Матрица :: MITS, но я не вижу SquareMatrix :: Smits инициализированное чем угодно.
В любом случае, эта линия
min.sMits[r-1][c] = Mits[r][c];
доступ SquareMatrix :: Smits в мин, но SquareMatrix :: Smits является неонициализированным, что приведет к исключению Segfault/Bad Access. Вы, вероятно, хотели получить доступ Матрица :: MITS, но не в состоянии, потому что Матрица :: MITS является частный, а также SquareMatrix Только наследует публичный Матрица.
Если вы хотите получить доступ SquareMatrix :: Smits в мин, вам нужно заполнить SMITS Каким -то образом, или дайте защищенный доступ к MIT, и вместо этого используйте его. т.е.
class Matrix
{
protected:
int** mits;
а потом
min.mits[r-1][c] = Mits[r][c];