@_13th_Dragon
Wiem, że bezpieczniej byłoby trzymać wszystko w jednym pliku, ale to już odgórne zarządzenie ćwiczeniowca, żeby dzielić.
C/C++ niestety nie jest moją najmocniejszą stroną.
Przerzuciłem to do jednego pliku, ale nadal ten operator wypisania powoduje masę błędów. Wrzucam zawartość obu plików, żeby można było to ogarnąć jako całość.
Array.h
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;
template<class Element>
class Array{
private:
int size;
Element** A;
void powieksz(int i);
public:
Array();
Array(const Array& B);
~Array();
int getSize();
bool operator= (const Array & B);
Array operator+ (const Array & B);
void dodaj(Element*);
void dodaj(int i, Element* e);
void usun(const int i);
void wypisz();
Element*& operator[] (const int i);
//-1 gdy nie ma e w tablicy lub pozycja e
int find(const Element* e);
template<Element>
friend ostream& operator<< (ostream& str, const Array<Element>& B);
};
template<typename Element>
struct Komp {
Komp() {}
int operator()(Array<Element>& a1, Array<Element>& a2) {
return a1.getSize() < a2.getSize();
}
};
template<class E>
ostream& operator<<(ostream& str, const Array<E>& B)
{
str << "Tablica zawiera "<<B.size<<" elementow:"<<endl;
for(int i = 0; i < B.size; i++)
{
if (B.A[i] == NULL)
cout << "NULL ";
else
cout << *B.A[i] << " ";
cout << endl;
}
return str;
}
#endif
I Array.cpp
#include "array.h"
#include <iostream>
using namespace std;
const int PRZYROST = 4;
template<class Element>
Array<Element>::Array()
{
cout<<"Konstruktor pustej tablicy"<<endl;
size = PRZYROST;
A = new Element*[size];
for(int i = 0; i < size; i++)
A[i] = NULL;
wypisz();
}
template<class Element>
Array<Element>::Array(const Array& B) {
cout<<"Konstruktor kopiujacy tablicy"<<endl;
size = B.size;
A = new Element*[size];
for(int i = 0; i < size; i++)
A[i] = B.A[i];
wypisz();
}
template<class Element>
int Array<Element>::getSize() {
return size;
}
template<class Element>
void Array<Element>::wypisz()
{
cout << "Tablica " << size << " elementowa: " <<endl;
for(int i = 0; i < size; i++)
{
if (A[i] == NULL)
cout << "NULL ";
else
cout << *A[i] << " ";
cout << endl;
}
}
template<class Element>
void Array<Element>::powieksz(int i)
{
i++;
int oldSize = size;
int newSize;
int reszta = (i % PRZYROST);
if (reszta == 0) newSize = i;
else if (reszta == 1) newSize = i + 3;
else if (reszta == 2) newSize = i + 2;
else if (reszta == 3) newSize = i + 1;
Element **newA = new Element*[newSize];
for (int j = 0; j < oldSize; j++)
newA[j] = A[j];
for (int j = oldSize; j < newSize; j++) {
newA[j] = NULL;
}
delete [] A;
A = newA;
size = newSize;
}
template<class Element>
Element*& Array<Element>::operator[] (const int i)
{
if (i < 0) {
throw "Ujemny wskaznik do tablicy - blad";
}
if (i < size) {
return A[i];
}
else {
powieksz(i);//powieksza i kopiuje
return A[i];
}
}
template<class Element>
Array<Element>::~Array()
{
delete[] A;
}
template<class Element>
void Array<Element>::dodaj(Element* element)
{
int i = 0;
while ((i < size) && (A[i] != NULL))
i++;
if (i == size) {
//nie ma wolnego miejsca
powieksz(size);
A[size] = element;
} else {
//jest wolne miejsce
A[i] = element;
}
}
template<class Element>
void Array<Element>:: dodaj(int i, Element* element)
{
if (i >= size) {
//nie ma wolnego miejsca
powieksz(i);
A[i] = element;
} else {
//jest wolne miejsce
A[i] = element;
}
}
template<class Element>
void Array<Element>::usun(const int i)
{
if (i < 0) {
throw "Ujemny wskaznik do tablicy - blad";
}
if (i < size)
A[i] = NULL;
}
template<class Element>
bool Array<Element>::operator= (const Array<Element> & B)
{
delete [] A;
size = B.size;
A = new Element* [size];
for (int i = 0; i < size; i++)
A[i] = B.A[i];
return true;
}
template<class Element>
Array<Element> Array<Element>::operator+ (const Array<Element> & B)
{
Array<Element> * C = new Array<Element>;
for (int i = 0; i < this->size; i++)
(*C)[i] = A[i];
for (int i = 0; i < B.size; i++)
(*C)[this->size + i] = B.A[i];
return *C;
}
template<class Element>
int Array<Element>::find(const Element* e) {
for (int i = 0; i < size; i++) {
if (*A[i] == *e) {
return i;
}
}
return -1;
}