Witam,
Staram się napisać kontener z szablonu, napisałem funkcję dodaj, i testuję ją dla typu int, lecz przy przekazaniu argumentów dostaje bład:
Error 1 error C2440: '=' : cannot convert from 'int' to 'int *'
Szablon klasy:
template<class TYP>
class Kontener{
private:
static int rozmiar;
TYP *element;
Kontener<TYP> *nastepny;
public:
Kontener();
~Kontener();
void dodaj(TYP &t);
void usun(int);
TYP& pobierz(int);
void wypisz();
};
static int rozmiar=0; //LICZNIK ILE JEST ELEMENTOW W KONTENERZE
Kod metody dodaj:
template<class TYP>
void Kontener<TYP>::dodaj(TYP &t){
Kontener<TYP> *nowy = new Kontener<TYP>;
if (rozmiar == 0) //NIE MA ELEMENTOW W KONTENERZE
{
nowy->element = t; //PRZYPISANIE NOWEMU przekazany element;
rozmiar++;
nowy->nastepny=0;
}else //JESLI LISTA JEST ZAPELNIONA
{
Kontener<TYP> *tmp= new Kontener<TYP>;
tmp = this;//tmp przypisujemy adres 1 elementu listy
while(tmp->nastepny != 0){
tmp = tmp->nastepny; //IDZ DO KOLEJNEGO ELEMENTU
}
tmp->nastepny->element = t;
rozmiar++;
tmp->nastepny->nastepny=0;
}
}
Wywolanie w main():
int *a = new int();
int *b = new int();
*a = 23;
*b = 35;
Kontener<int> *lista;
lista = new Kontener<int>;
lista->dodaj(*a);
lista->dodaj(*b);
Jak to obejść, co należy poprawić?Proszę o wskazówki