0

我正在尝试为项目编写一个矩阵类。我将方阵分离为派生类。但是当我尝试对行列式执行次要操作时,我无法将矩阵成员分配给我的派生类对象。我该如何执行这些操作?实际上,我尝试了很多,我无法成功。这是代码;

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();
}
4

1 回答 1

1

这段代码有很多问题,但我认为特别的问题是minSquareMatrix,并且SquareMatrix构造函数调用Matrix构造函数,它可能会初始化Matrix::mits,但我没有看到SquareMatrix::sMits被初始化任何事物。

反正这条线

min.sMits[r-1][c] = Mits[r][c];

min中访问SquareMatrix::sMits,但SquareMatrix::sMits未初始化,这将导致段错误/错误访问异常。您可能打算访问Matrix::mits,但无法访问,因为Matrix:: mits是private,而SquareMatrix仅继承public Matrix

如果您想在min中访问SquareMatrix::sMits,您需要以某种方式填充sMits,或者授予对 mits 的受保护访问权限,然后改用它。IE

class Matrix
{
 protected:
     int** mits;

进而

min.mits[r-1][c] = Mits[r][c];
于 2011-12-02T23:38:15.000 回答