想问问为啥错了。。想不明白
#ifndef CLASS_H#define CLASS_H
#include <iostream>
#include <cstdlib> //(rand()%10)
#include <ctime>
using namespace std;
class Matrix
{
private:
int row;
int column;
int* data;
public:
Matrix(int row2, int column2);
Matrix();
~Matrix();
Matrix operator+ (const Matrix& a) const;
Matrix operator- (const Matrix& a) const;
Matrix operator* (const Matrix& a) const;
friend ostream& operator<< (ostream& out, Matrix& a);
};
Matrix::Matrix(int row2, int column2)
{
srand((int)time(0));
row = row2;
column = column2;
int i = 0, x = 0;
int** p;
p = new int* [row2]; //行 //申请行的空间
//每行的列申请空间
for (i = 0; i < row; i++)
{
p[i] = new int[column];
}
while (i < row2)
{
while (x < column)
{
p[i][x]=rand()%10;
x++;
}
i++;
}
data = p[row2];
}
Matrix::Matrix()
{
row = 0;
column = 0;
data = NULL;
}
Matrix::~Matrix()
{
delete []data;
}
Matrix Matrix::operator+ (const Matrix& a) const
{
Matrix A(row, column);
int i = 0,x =0;
while (i < row )
{
while (x < 4)
{
A.data[i][x] = data[i][x] + a.data[i][x];
x++;
}
i++;
}
return A;
}
Matrix Matrix::operator- (const Matrix& a) const
{
Matrix B(row, column);
int i = 0,x = 0;
while (i < row)
{
while (x < column)
{
B.data[i][x] = data[i][x] + a.data[i][x];
x++;
}
i++;
}
return B;
}
Matrix Matrix::operator* (const Matrix& a) const
{
Matrix C(row, a.column);
int i = 0, x = 0;
while (i < row)
{
while (x < column)
{
C.data[i][x] = data[row][x] * a.data[i][a.column];
x++;
}
i++;
}
return C;
}
ostream& operator<< (ostream& out, Matrix& a)
{
int i = 0;
while (i < a.row * a.column)
{
out << *(a.data + i) << " ";
if (a.column / 4 + 1 == a.column) /**/
{
out << endl;
}
}
return out;
}
#endif