При компиляции программа(MS Visual C++ 2010) выдаёт множество ошибок, а в конце сообщает "...mass.h(129): fatal error C1903: не удается восстановить после предыдущих ошибок; остановка компиляции"
Переопределение вывода не принимается в друзья класса.
Оператор random не определяется, хотя stdlib.h включена.
Вот основной код
Код:
/*1. Создать класс для хранения одномерных целочисленных массивов.
Обеспечить возможность задания количества элементов и базовой индексации.
Запрограммировать методы поиска элементов и сортировки. Перегрузить операции
для сложения и вычитания векторов. Перегрузить операцию индексирования с проверкой допустимости индекса.*/
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include "mass.h"
using std::setprecision;
int main(int argc, char* argv[])
{ A <int> a(10,1), sum(10,1), c(10,1), razn(10,1);
cout << "a = " << a;
int B=1;
int U,T;
a.funrand(1,10);
cout << "a = " << a;
int k=a.search(4);
if (k==B-1)
cout << "Element 4 was not found" << endl;
else cout << "Element 4 has a number " << k << endl;
a.sort();
cout << "a = " << a;
try
{ for (int i=B; i<10+B; i++)
c[i]=random(10);
cout << "c = "; cout<<c;
sum=a+c;
razn=a-c;
cout << "+ = " << sum;
cout << "- = " << razn;
cout << "a[8]= " << a[8] << endl;
cout << "a[2000000000]= " << a[2000000000] << endl;
}
catch (AException e)
{ cout << e.mes << endl;}
cout<<"_______________________________________________________________"<<endl << endl;
A <double> q(10,1), summa(10,1), p(10,1), raznost(10,1);
cout << "q = " << setprecision(4) << q;
for (int i=B; i<10+B; i++)
q[i]=(double)rand()/RAND_MAX*(9)+1;
cout << "q = " << setprecision(3) << q;
k=q.search(8);
if (k==B-1)
cout << "Element 8 was not found" << endl;
else cout << "Element 8 has a number " << k << endl;
q.sort();
cout<< "q = " << setprecision(3) << q;
try
{ for (int i=B; i<10+B; i++)
p[i]=(double)rand()/RAND_MAX*(9)+1;
cout << "p = " << setprecision(3) << p;
summa=q+p;
raznost=q-p;
cout << "+ = " << setprecision(3) << summa;
cout << "- = " << setprecision(3) << raznost;
cout << "q[8]= " << q[8] << endl;
cout << "q[20]= " << q[20] << endl;
}
catch (AException e)
{ cout << e.mes << endl; }
getchar();getchar();
return 0;
}
И заголовочный файл
Код:
#ifndef __MASS_H
#define __MASS_H
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::setw;
using std::dec;
using std::endl;
class AException
{ public:
char mes[256];
AException (char *m) {strcpy (mes,m);}
};
template <class Type>
class A
{ Type *a;
int N, B;
public:
A (int,int);
int search(Type);
void funrand(int,int);
void sort();
friend ostream& operator << (ostream& s, A <Type> aaa)
{ cout<<"(";
for (int i=aaa.B; i<aaa.N+aaa.B; i++)
s<<setw(2)<<dec<<aaa.a[i]<<" ";
s<<")"<<endl;
return s;
}
A operator + (A<Type>);
A operator - (A<Type>);
A operator = (A<Type>);
Type& operator [] (int);
~A();
A (const A&);
};
//int error=-9999999;
template <class Type>
A <Type>::A (int n, int b=1)
{ N=n;
a=(Type*)calloc(N,sizeof(Type));
B=b;
a-=B;
randomize();
}
template <class Type>
A <Type>::A(const A &V)
{ N=V.N;
B=V.B;
a=(Type*)calloc(N,sizeof(Type));
a-=B;
for (int i=B; i<B+N; i++)
a[i]=V.a[i];
}
template <class Type>
int A <Type>::search(Type x)
{ int k=B-1;
for (int i=B; i<N+B; i++)
if (a[i]==x)
{k=i; break;}
return k;
}
template <class Type>
void A<Type>::funrand(int U, int T)
{ for (int i=U; i<=T; i++)
a[i]=random(10+1)-1;
}
template <class Type>
void A <Type>::sort()
{ for (int i=B; i<N+B-1; i++)
for (int j=i+1; j<N+B; j++)
if (a[i]>a[j])
{ Type tmp;
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
template <class Type>
A<Type> A<Type>::operator + (A <Type> c)
{ if ((c.B!=B)||(c.N!=N))
{ A err(N);
return err;
}
A sum(N,B);
for (int i=B; i<B+N; i++)
sum.a[i]=a[i]+c.a[i];
return sum;
}
template <class Type>
A<Type> A<Type>::operator - (A <Type> c)
{ if ((c.B!=B)||(c.N!=N))
{ A err(N);
return err;
}
A razn(N,B);
for (int i=B; i<B+N; i++)
razn.a[i]=a[i]-c.a[i];
return razn;
}
template <class Type>
A<Type> A<Type>::operator = (A <Type> V)
{ a+=B;
a=(Type*) realloc(a, V.N*sizeof(Type));
N=V.N;
B=V.B;
a-=B;
for (int i=B; i<B+N; i++)
a[i]=V.a[i];
return V;
}
template <class Type>
Type& A <Type>::operator [] (int i)
{ char s[10];
char s2[30]="Wrong index ";
if (i<B || i>=B+N)
{ sprintf (s,"%d",i);
strcat (s2,s);
throw AException(s2);
}
return a[i];
}
template <class Type>
A <Type>::~A()
{ a+=B;
free (a);
}
#endif