I can not understand why the program crashes when a function is called:
template <class T>void set<T>::del(elem<T> *x){ if(count==1){ head=tail=NULL; count=0; }else{ if(x==head){ head=head->next; }else{ elem<T> *tmp=head; if(x==tail){ while(!(tmp->next==tail)){ tmp=tmp->next; } tail=tmp; }else{ while(!(tmp->next==x)){ tmp=tmp->next; } tmp->next=x->next; } } } //delete x->data; delete x; } In the place of commenting, when uncommenting falls. If you write with a change - falls. But when this is given the same value of the object, and not a link, or am I confusing something?
template <class T> struct elem{ //Элемент списка T* data; elem* next; }; template <class T> struct set{ public: set():count(0),head(NULL),tail(NULL){}; set(set<T> &s); set(int c, T &m); // ~set(); //Деструктор void test(T* x); private: elem<T> *head; //Голова elem<T> *tail; //Хвост int count; //Количество элементов void del(elem<T> *x); //Удаление по адрессу элемента }; template <class T>elem<T> * set<T>::addh(T *x){ //Добавление в голову (протестировано) elem<T> *temp = new elem<T>; //Указатель на временный элемент temp->data=x; //Записываем указатель во временный элемент temp->next=head; //Записываем следующим элементом голову count++; return temp; //Возвращаем указатель на новый элемент }
хwas created, which is passed to theaddh()function. - gecube