Witam, mam problem z przeciążaniem operatorów. Popatrzmy na main - > jest tam kilka rzeczy prezentujących to co się dzieje w klasie Lista. problem mam z operatorem "=". jak widać, w mainie są 2 instrukcje: cout<<"lista 3=1+2: "<<a+b<<endl; oraz c=a+b; pierwsza działa bezproblemowo, jednak na drugiej program się zawiesza (SIGSEGV). po puszczeniu debuggera problem wykrywa w 141 linijce (while (pom->next)). dodatkowa rzecz: mam znaczniki cout<<"A" i cout<<"B" w operatorze + zeby widziec co się dzieje, i jesli uruchomimy program z w.w. instrukcjami, i powiedzmy wpiszemy do 1 listy 2 elementy a do drugiej 3 to pokazuje "AABBB" i "BBB", a powinno "AABBB" "AABBB"; nie mam pojęcia gdzie może być błąd. jeśli ktoś mógłby pomóc to byłbym bardzo wdzięczny :)
/*
1) klasa lista
2) operatory >>(dopisuje 1 element),<<,+(dodaje listy),=(przepisuje wartosci)
3) konstruktory, destruktor (nie stosowac rekurencji)
*/
#include <iostream>
#include "operatory.h"
using namespace std;
template <class T>
class Lista;
template <class T>
class element{
public:
//friend class Lista; //dzieki temu mamy dostep do prywatnych el. class lista
T wart;
element<T>* next;
};
template <class T>
class Lista {
public:
//friend class element;
element<T>* head;
Lista(){
head=NULL;
}
Lista<T> & operator = (Lista <T> o1);
Lista<T> & operator + (Lista<T> o1);
//friend istream & operator >> (istream & s, Lista <T> & l);
//friend ostream & operator << (ostream & s, Lista <T> & l);
~Lista(){
element<T> *pom=head;
element<T> *pom2;
while (pom)
{
cout<<endl<<"zwolniony adres: "<<pom;
pom2=pom->next;
delete pom;
pom=pom2;
}
cout<<endl<<endl;
}
};
template <class T>
istream & operator >> (istream & s, Lista <T> & l){
element<T> *nowy,*o2=l.head;
nowy=new element<T>;
nowy->next=NULL;
s>>nowy->wart;
cout<<endl<<"zapisano element pod adresem "<<nowy<<endl<<endl;
if (l.head==NULL) {
l.head=nowy;
}
else {
while (o2->next){
o2=o2->next;
}
o2->next=nowy;
}
return s;
}
template <class T>
ostream & operator << (ostream & s, Lista<T> & l){
element<T> *o1=l.head;
if (l.head==NULL) s<<"brak elementow";
while (o1){
s<<o1->wart;
o1=o1->next;
}
return s;
}
template <class T>
Lista<T> & Lista<T>::operator = (Lista<T> o1){
static Lista<T> nowa;
element<T> *pom=nowa.head;
while (o1.head){
element<T>* nowy=new element<T>;
nowy->wart=o1.head->wart;
nowy->next=NULL;
if (o1.head->next)
o1.head=o1.head->next;
if (!nowa.head)
continue;
pom->next=nowy;
cout<<"C";
}
return nowa;
}
template <class T>
Lista<T> & Lista<T>:: operator + (Lista<T> o1){
static Lista<T> nowa;
nowa.head=NULL;
while (head){
element<T>* nowy=new element<T>;
element<T>* pom=nowa.head;
nowy->wart=head->wart;
nowy->next=NULL;
if (nowa.head==NULL){
nowa.head=nowy;
}
else{
while (pom->next){
pom=pom->next;
}
pom->next=nowy;
}
head=head->next;
cout<<"A";
}
while (o1.head){
element<T>* nowy=new element<T>;
element<T>* pom=nowa.head;
nowy->wart=o1.head->wart;
nowy->next=NULL;
if (nowa.head==NULL)
nowa.head=nowy;
else {
while (pom->next)
pom=pom->next;
pom->next=nowy;
}
o1.head=o1.head->next;
cout<<"B";
}
return nowa;
}
int main (){
{
Lista<int> a,b,c;
cout<<"ile elementow listy chcesz wpisac?: ";
int ile_elementow;
cin>>ile_elementow;
cout<<"wpisz elementy: ";
for (int i=0;i<ile_elementow;i++){
cin>>a;
}
cout<<"ile elementow chcesz wpisac?: ";
cin>>ile_elementow;
cout<<"wpisz elementy: ";
for (int i=0;i<ile_elementow;i++){
cin>>b;
}
cout<<"lista 1: "<<a;
cout<<endl;
cout<<"lista 2: "<<b;
cout<<endl;
cout<<"lista 3=1+2: "<<a+b<<endl;
c=(a+b);
cout<<"lista 3=1+2: "<<c<<endl;
c=a+b;
cout<<"po ponownym dodaniu: "<<c<<endl;
/*Lista <macierze> a,b;
cout<<"wpisz 1 macierz (pierwszy wymiar): ";
cin>>a;
cout<<"wpisz 2 macierz (pierwszy wymiar): ";
cin>>a;
cout<<a;*/
//c=(a+b);
//cout<<endl<<"lista 3=1+2: ";
//c=(pt[0]+pt[1]);
//cout<<"po ponownym dodaniu: ";
//cout<<c;
}
return 0;
}
<code>
<code=cpp></code>
albo<cpp></cpp>