侯捷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;
}
报错信息: