Potrzebuje takiej umiwersjalnej klasy, tzn Listy dwukierunkowej..
Ale chce by kasa albo przehcowywala jakas strukturke albo stringa lub cokolwiek a nie chce pisac dla kazdego typu oddzielne listy..
Calosc jest prosta.. Robie szablon klasy majaca wskazniki next i prev oraz Dane:
template<typename T> class Lista2D {
public:
unsigned int Index;
Lista2D<T> *next;
Lista2D<T> *prev;
T Dane;
T &operator[](unsigned int Element) throw (ErrorClass) {
Lista2D<T> *temp = this;
while (((*temp).Index != Element) && (temp->next)) {
if ((*temp).Index < Element)
temp = temp->next;
else if ((*temp).Index > Element)
temp = temp->prev;
else
throw ErrorClass(BLAD_NiemozliwaSytuacja);
}
return (*temp).Dane;
}
inline bool isEmpty(){ return !next; };
~Lista2D<T>() { Wyczysc(); }
Lista2D<T>() {next = NULL; prev = NULL; Index = 0;};
void Wyczysc() {
//rozpoczynam czyszczenie dynamicznej pamieci
// 1. przesuwam sie na sam koniec lancuszka
Lista2D<T> *temp = this;
while (temp->next)
temp = temp->next;
// 2. cofam sie az do "this" kasujac liste
while (temp != this){
temp = temp->prev; // cofniecie
delete temp->next; // usuniecie
temp->next = NULL; // a na wszelki wypadek ;-)
}
}
};
(moj pierwszy szablon ;-) ) teraz chcialem ten szablon classy odziedziczyc w :
class Plik: Lista2D<string> {
public:
void Init(const string &) throw (ErrorClass);
};
void Plik::Init(const string &NazwaPliku) throw(ErrorClass){
if ((next) || (prev))
Wyczysc();
ifstream plik(NazwaPliku.c_str());
if (plik.fail())
throw ErrorClass(BLAD_OtwarciePliku, NazwaPliku);
char buf[257];
string tekst;
Lista2D<string> *temp = this;
while (!plik.eof()) {
plik.getline(buf, 256);
buf[plik.gcount()] = '\0'; // <- zakanczam stringa ;-)
tekst = buf;
temp->next = new Plik;
if (!this->next) // na sprawdzenie czy pamiec przydzielono
throw ErrorClass(BLAD_MemAlocation, "Plik::Init(), while()");
temp->next->prev = temp;
temp->next->next = NULL;
(*temp->next).Dane = tekst;
(*temp->next).Index = (*temp).Index + 1;
temp = temp->next;
};
}
Wszystko ladnie i pieknie...
ale skompilowac sie nie chce...
Plik Config;
Config.Init("config.ini");
Config[1];
a kompilator wywala:
`T& Lista2D<T>::operator[](unsigned int) [with T = std::string]' is inaccessible within this context
`Lista2D<std::string>' is not an accessible base of `Plik'
Moze wiecie jak to naprawic?
Z gory dziekuje za odpowiedz...