@Nazarko03

Как исправить ошибку в коде?

#include<vector>
#include<iostream>
using namespace std;
 
class Matrix
{
private:
    vector<vector<int>> Mat;
    Matrix() {};
public:
    Matrix(vector<vector<int>> mat) : Mat(mat) {};//Конструктор
    ~Matrix() {};
    Matrix(const Matrix &rhs);
 
 
    int getnum(int r, int c)const 
    { 
        return Mat[r][c]; 
    };
 
    int getrow()const 
    {
        return (Mat.size()); 
    };
 
    int getcol()const 
    { 
        return (Mat[0].size()); 
    };
 
    //операторы
    friend ostream & operator<<(ostream &os, const Matrix &rhs);
    Matrix Matrix::operator+(const Matrix &rhs);
    Matrix &operator=(const Matrix &rhs);
    //Matrix &operator*(const Matrix &lhs, const Matrix &rhs);
    //Matrix &operator*(const Matrix &lhs, const vector<int> &rhs);
    
};
ostream &operator<<(ostream &os, const Matrix &rhs)
{
    for (int i=0; i < rhs.getrow(); ++i)
    {
        for (int j=0; j < rhs.getcol(); ++j)
            os << rhs.getnum(i, j) << '\t';
        os << endl;
    }
    return os;
}
Matrix Matrix::operator+(const Matrix &rhs)
{
    vector<vector<int>> temp(getrow(), vector<int>(getcol(), 0));
    Matrix Temp(temp);
    if ((getrow() != rhs.getrow()) || (getcol() != rhs.getcol()))
    {
        cout << "Размеры матриц не совпадают." << endl;
        system("pause");
        exit(1);
    }
    for (unsigned int i=0; i < Temp.getrow(); i++)
        for (unsigned int j=0; j < Temp.getcol(); j++)
            Temp.Mat[i][j] = getnum(i, j) + rhs.getnum(i, j);
    return Temp;
}
Matrix &Matrix::operator=(const Matrix &rhs)
{
    cout << rhs.getrow() << endl;
    this->Mat.resize(rhs.getrow());
    for (int i=0; i < this->getrow(); ++i)
        this->Mat[i].resize(rhs.getcol());
    for (int i(0); i < this->getrow(); ++i)
        for (int j=0; j < this->getcol(); ++j)
            this->Mat[i][j] = rhs.Mat[i][j];
    return *this;
}
 
Matrix::Matrix(const Matrix &rhs)
{
    Mat = rhs.Mat;
}
int main()
{
    setlocale(LC_ALL, "rus");
    int n, n1;
    cin >> n >> n1;
    vector<vector<int>> A(n, vector<int>(n1, 0));
    vector<vector<int>> B(n, vector<int>(n1, 0));
    vector<vector<int>> C(1,vector<int>(1, 0));
    for (int i=0; i <n1; ++i)
        for (int j=0; j <n; ++j)
        {
            A[i][j] = i;
            B[j][i] = i;
        }
    Matrix test(A);
    Matrix test2(B);
    Matrix test3(C);//сложение
 
    /*
    Matrix test4(D);//умножение матриц
    Matrix test5(E);//умножение матрицы на вектор
    */
 
    cout << "Первая матрица:" << endl << test << " " << endl;
    cout << "Вторая матрица:" << endl << test2 << " " << endl;
    test3 = test + test2;
    cout<<"Сложение 1 и 2:"<<endl << test3;
 
    system("pause");
    return 0;
}
  • Вопрос задан
  • 58 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Mercury13
Программист на «си с крестами» и не только
1. Op= излишня, такая конструкция сама себя присвоит. С деструктором аналогично.
2. Op+ — лучше пользоваться исключениями.
3. Странное у вас устройство матриц — и не совсем константные, и менять нельзя.
4. A[i][j] = i; — возможен выход за пределы массива.
5. Если вывод матриц многострочный — подобный вывод не слишком удачный.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы