309摸鱼巨人 2022-10-20 16:15
浏览 5
已结题

侯捷Essential c++中练习4.5中遇到的问题,最后运行的时候报错了

侯捷Essential c++中练习4.5中遇到的问题,最后运行的时候报错了

Matix.h

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <string>

using namespace std;

typedef float elemType;

class Matrix
{
    friend Matrix operator*(const Matrix&, const Matrix&);
    friend Matrix operator+(const Matrix&, const Matrix&);
public:
    Matrix();
    Matrix(const elemType*);
    Matrix(elemType a1, elemType a2, elemType a3, elemType a4, elemType a5, elemType a6, elemType a7, elemType a8,
           elemType a9, elemType a10, elemType a11, elemType a12, elemType a13, elemType a14, elemType a15, elemType a16);

    int row() const { return 4; }
    int cols() const { return 4; }
    void operator+=(const Matrix&);

    ostream& print(ostream&) const;
    
    elemType operator()(int row, int column) const
    {
        return _matrix[row][column];
    }
    elemType& operator()(int row, int column)
    {
        return _matrix[row][column];
    }

private:
    elemType _matrix[4][4];
};

#endif // !MATRIX_H

Matix.cpp

 #include "Matrix.h"

Matrix operator*(const Matrix& m1, const Matrix& m2)
{
    Matrix result;
    for (int ix = 0; ix < m1.row(); ++ix)
        for (int jx = 0; jx < m2.cols(); ++jx)
        {
            result(ix, jx) = 0;
            for (int kx = 0; kx < m1.cols(); ++kx)
                result(ix, jx) += m1(ix, kx) * m2(kx, jx);
        }

    return result;
}

inline
ostream& operator<<(ostream& os, const Matrix& m)
{
    return m.print(os);
}

Matrix operator+(const Matrix& m1, const Matrix& m2)
{
    Matrix result(m1);
    result += m2;
    return result;
}

Matrix::Matrix()
{
    _matrix[0][0] = 0.; _matrix[0][1] = 0.; _matrix[0][2] = 0.; _matrix[0][3] = 0.;
    _matrix[1][0] = 0.; _matrix[1][1] = 0.; _matrix[1][2] = 0.; _matrix[1][3] = 0.;
    _matrix[2][0] = 0.; _matrix[2][1] = 0.; _matrix[2][2] = 0.; _matrix[2][3] = 0.;
    _matrix[3][0] = 0.; _matrix[3][1] = 0.; _matrix[3][2] = 0.; _matrix[3][3] = 0.;
}

Matrix::Matrix(const elemType* array)
{
    int array_index = 0;
    for (int ix = 0; ix < 4; ++ix)
        for (int jx = 0; jx < 4; ++jx)
            _matrix[ix][jx] = array[array_index++];
}

inline
Matrix::Matrix(elemType a1, elemType a2, elemType a3, elemType a4, elemType a5, elemType a6, elemType a7, elemType a8,
    elemType a9, elemType a10, elemType a11, elemType a12, elemType a13, elemType a14, elemType a15, elemType a16)
{
    _matrix[0][0] = a1; _matrix[0][1] = a2; _matrix[0][2] = a3; _matrix[0][3] = a4;
    _matrix[1][0] = a5; _matrix[1][1] = a6; _matrix[1][2] = a7; _matrix[1][3] = a8;
    _matrix[2][0] = a9; _matrix[2][1] = a10; _matrix[2][2] = a11; _matrix[2][3] = a12;
    _matrix[3][0] = a13; _matrix[3][1] = a14; _matrix[3][2] = a15; _matrix[3][3] = a16;
}

ostream& Matrix::print(ostream& os) const
{
    int cnt = 0;
    for (int ix = 0; ix < 4; ++ix)
        for (int jx = 0; jx < 4; ++jx, ++cnt)
        {
            if (cnt && !(cnt % 8)) os << endl;
            os << _matrix[ix][jx] << ' ';
        }
    os << endl;
    return os;
}


void Matrix::operator+=(const Matrix& m)
{
    for (int ix = 0; ix < m.row(); ++ix)
        for (int jx = 0; jx < m.cols(); ++jx)
            _matrix[ix][jx] += m(ix, jx);
}

main.cpp

#include <iostream>
#include <string>
#include "Matrix.h"

using namespace std;

ostream& operator<<(ostream& os, const Matrix& m);

int main()
{
    Matrix m;
    cout << m << endl;

    return 0;
}

报错信息:

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 10月28日
    • 创建了问题 10月20日

    悬赏问题

    • ¥15 孟德尔随机化怎样画共定位分析图
    • ¥18 模拟电路问题解答有偿
    • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
    • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
    • ¥15 误删注册表文件致win10无法开启
    • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
    • ¥60 ESP32怎么烧录自启动程序
    • ¥50 html2canvas超出滚动条不显示
    • ¥15 java业务性能问题求解(sql,业务设计相关)
    • ¥15 52810 尾椎c三个a 写蓝牙地址