Задача: Класс для работы с матрицами: операторы +, =, * на число, когда число находится справа и слева, операторы ввода-вывода.
Преподаватель требует, чтобы в опраторе сложения применялся конструктор с параметрами, но вылезает ошибка... Подскажите пожалуйста, где что я неправильно делаю!
| Код | class Cmas_2D { int n; int m; int *A;
public: Cmas_2D (void); Cmas_2D (int nn, int mm); Cmas_2D (Cmas_2D &); void scan_2D (); ~Cmas_2D (void);
///>>, <<, =, +, x*A, A*x ///
friend istream& operator >> (istream &in, Cmas_2D &t); friend ostream& operator << (ostream &out, Cmas_2D &t); Cmas_2D& operator = (Cmas_2D &B); Cmas_2D operator + (Cmas_2D); friend Cmas_2D operator * (int x, Cmas_2D & B); Cmas_2D operator * (int x); }; //konstruktory// . . .
//friendy & operatory//
istream& operator >> (istream &in, Cmas_2D &t) { cout<<"Vvedite matritsu"<<endl; for (int i=0; i<t.n; i++) { for (int j=0; j<t.m; j++) in>>t.A[i*t.n+j]; } return in; };
ostream& operator << (ostream &out, Cmas_2D &t) { int i, j; for (i=0; i<t.n; i++) { for (j=0; j<t.m; j++) out<<t.A[i*t.m+j]<<"\t"; out<<endl; } return out; };
Cmas_2D& Cmas_2D::operator = (Cmas_2D &B) { delete[] A; A=NULL; n=B.n; m=B.m; A=new int[n*m]; for (int i=0; i<n; i++) { for (int j= 0; j<m; j++) { A[i*n+j]=B.A[i*n+j]; } } return *this; }
Cmas_2D Cmas_2D::operator + (Cmas_2D C) { int i=0, j=0; Cmas_2D B (n, m); if (n==C.n && m==C.m) { for (i=0; i<n; i++) { for ( j=0; j<m; j++) { B.A[i*n+j]=Cmas_2D::A[i*n+j]+C.A[i*n+j]; } } return B; } else cout<<"Matritsy slozhit nelza"<<endl; return *this; }
Cmas_2D operator * (int x, Cmas_2D &B) { cout<<"Vvedite chislo"<<endl; cin>>x; int i, j; Cmas_2D C; for (i=0; i<C.n; i++) { for (j=0; j<C.m; j++) { C.A[i*C.n+j]=B.A[i*C.n+j]*x; } } return C; }
Cmas_2D Cmas_2D::operator * (int x) { cout<<"Vvedite chislo"<<endl; cin>>x; Cmas_2D B; int i; for (i=0; i<n; i++) { for (int j=0; j<m; j++) { B.A[i*n+j]=A[i*n+j]*x; } } return B; }
//funktsii//
void Cmas_2D::scan_2D () { srand((unsigned)time(NULL)); int i, j; int min=-100, max=100; for (i=0; i<n; i++) { for (j=0; j<m; j++) A[i*m+j]=rand()%(max-min+1)+min; } }
//destruktor// . . .
/////////////////////////////////////////////////////////////
void main (void) { ///////////slozhenie cout<<"*slozhenie*"<<endl; Cmas_2D B; B.scan_2D(); cout<<B<<endl; Cmas_2D C; C.scan_2D(); cout<<C<<endl; //int n, m; Cmas_2D C1; //(n, m); C1=B+C; cout<<"B + C= "<<endl<<С1<<endl; ///////////x*A cout<<"*umnozhenie*"<<endl; cout<<"(method)"<<endl; cin>>B; B=B*2; cout<<"B * x = "<<endl<<B; ///////////A*x cout<<"*umnozhenie*"<<endl; cout<<"(friend)"<<endl; cin>>B; B=2*B; cout<<"x * B = "<<endl<<B; }
|
|