Poslao: 02 Dec 2013 15:12
|
offline
- feniks13
- Novi MyCity građanin
- Pridružio: 18 Jan 2013
- Poruke: 13
|
Nikako da rijesim problem konkatencije dva stringa pomocu tri objekta. Inicializovao bih prva dva objekta pa preko preklopljenog operatora "+" bih da spojim ta dva objekta pa da dodijelim trecem objektu. Uradio sam na naci da spojim dva objekta preko preklopljenog operatora "+=". Kod izgleda ovako
- #include<iostream>
- #include<string.h>
- #include<stdlib.h>
- using namespace std;
- class Tekst
- {
- friend ostream &operator<<(ostream &,const Tekst &);
- friend istream &operator>>(istream &,Tekst &);
- public:
- Tekst();
- Tekst(const char *);
- Tekst(const Tekst &);
- Tekst &operator=(Tekst &t);
- Tekst &operator+(Tekst);
- Tekst &operator+=(Tekst);
- ~Tekst();
- private:
- char *txt;
- };
- ostream &operator<<(ostream &out,const Tekst &t)
- {
- out<<t.txt; return out;
- }
- istream &operator>>(istream &in,Tekst &t)
- {
- in>>t.txt; return in;
- }
- Tekst::Tekst()
- {
- txt=new char(0);
- }
- Tekst::Tekst(const char *t)
- {
- txt=new char[strlen(t)+1];
- strcpy(txt,t);
- }
- Tekst::Tekst(const Tekst &t)
- {
- txt=new char[strlen(t.txt)+1];
- strcpy(txt,t.txt);
- }
- [color=red]Tekst &Tekst::operator=(Tekst &t)
- {
- if(this!=&t)
- {
- delete [] txt;
- txt=new char(strlen(t.txt)+1);
- strcpy(txt,t.txt);
- }
- return *this;
- }
- Tekst &Tekst::operator+(const Tekst t)
- {
- Tekst r(t);
- int n; n=strlen(txt)+1;
- r.txt=(char*)realloc(r.txt,n*sizeof(char));
- //strcat(r.txt," ");
- strcat(r.txt,txt);
- return r;
- }
- Tekst &Tekst::operator+=(Tekst t)
- {
- int n; n=strlen(t.txt)+strlen(txt)+1;
- txt=(char*)realloc(txt,n*sizeof(char));
- strcat(txt," ");
- strcat(txt,t.txt);
- return *this;
- }[/color]
- Tekst::~Tekst(){ delete [] txt;}
- main()
- {
- int i=0;
- Tekst a,b("Beograd"),c(b),d,f;
- cout<<"a: "; cout<<a<<endl;
- cout<<"b: "; cout<<b<<endl;
- cout<<"c: "; cout<<c<<endl;
- d=b;
- cout<<"d: "; cout<<d<<endl;
- c+=d;
- cout<<"Prva konkatenacija: "<<c;
- f=c+d;
- cout<<"\nDruga konkatenacija: "<<f;
-
- }
Kriticni dio je ovaj:
- Tekst &Tekst::operator+(const Tekst t)
- {
- Tekst r(t);
- int n; n=strlen(txt)+1;
- r.txt=(char*)realloc(r.txt,n*sizeof(char));
- //strcat(r.txt," ");
- strcat(r.txt,txt);
- return r;
- }
Ako neko ima resenje neka ponudi.. unaprijed hvala
|
|
|
Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
|
|
Poslao: 02 Dec 2013 15:55
|
offline
- morando

- Građanin
- Pridružio: 19 Maj 2011
- Poruke: 297
|
Ne mixuj realloc sa new/delete!!!
Za operatore +, += argument treba da bude referenca
- Tekst& Tekst::operator+(const Tekst& t)
- {
- char* temp = new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(temp, txt);
- delete [] txt;
- strcat(temp, t.txt);
- txt = temp;
- return *this;
- }
Pisem napamet, mislim da nisam pogresio.
|
|
|
|
Poslao: 02 Dec 2013 16:11
|
offline
- feniks13
- Novi MyCity građanin
- Pridružio: 18 Jan 2013
- Poruke: 13
|
Hvala na odgovoru, znam da ne bi trebalo mijesati, na to nas je i profesor upozoravao, ali u nedostatku ideja posegnuo sam za nekim ne tako dobrim resenjima. Imas samo malu gresku u kodu stavljao si samo 't' kao argument kod funkcije 'strlen' i 'strcat' a trebalo je 't.txt', nakon te prepravke program radi eksta. Jos jednom hvala na pomoci.
- Tekst &Tekst::operator+(const Tekst& t)
- {
- char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(temp, txt);
- delete [] txt;
- strcat(temp, t.txt);
- txt = temp;
- return *this;
- }
|
|
|
|
Poslao: 02 Dec 2013 16:41
|
offline
- morando

- Građanin
- Pridružio: 19 Maj 2011
- Poruke: 297
|
Ipak sam se zeznuo, operator + treba da vrati privremen objekat dok += moze da ostane kao sto sam napisao. Kad zavrsim neka posla napisacu kod da valja, mada moze neko i u medjuvremenu da pomogne.
|
|
|
|
Poslao: 02 Dec 2013 16:49
|
offline
- feniks13
- Novi MyCity građanin
- Pridružio: 18 Jan 2013
- Poruke: 13
|
Uradio sam ja, al' sam zaboravio da azuriram odgovor .
Kod izgleda ovako:
- Tekst &Tekst::operator+(const Tekst& t)
- {
- Tekst k;
- k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(k.txt, txt);
- strcat(k.txt, t.txt);
- return k;
- }
ili sva tri operatora '+','=','+='
- Tekst &Tekst::operator=(Tekst &t)
- {
- if(this!=&t)
- {
- delete [] txt;
- txt=new char(strlen(t.txt)+1);
- strcpy(txt,t.txt);
- }
- return *this;
- }
- Tekst &Tekst::operator+(const Tekst& t)
- {
- Tekst k;
- k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(k.txt, txt);
- strcat(k.txt, t.txt);
- return k;
- }
- Tekst &Tekst::operator+=(Tekst t)
- {
- char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(temp, txt);
- delete [] txt;
- strcat(temp, t.txt);
- txt = temp;
- return *this;
- }
|
|
|
|
Poslao: 02 Dec 2013 17:15
|
offline
- morando

- Građanin
- Pridružio: 19 Maj 2011
- Poruke: 297
|
Samo male izmene da bude "savrseno":
- // ovo
- Tekst &Tekst::operator=(Tekst &t)
-
- // u ovo, jer ne menjamo t objekat
- Tekst &Tekst::operator=(const Tekst &t)
- // ovo
- Tekst& Tekst::operator+(const Tekst& t)
-
- // u ovo jer ne mozes vratiti referencu na privremen objekat (bez posledica)
- Tekst Tekst::operator+(const Tekst& t)
- // ovo
- Tekst &Tekst::operator+=(Tekst t)
-
- // u ovo
- Tekst &Tekst::operator+=(const Tekst& t)
|
|
|
|
Poslao: 02 Dec 2013 17:39
|
offline
- feniks13
- Novi MyCity građanin
- Pridružio: 18 Jan 2013
- Poruke: 13
|
Kad prebacim ovo
- Tekst& Tekst::operator+(const Tekst& t)
u ovo
- Tekst Tekst::operator+(const Tekst& t)
program mi "pukne". Odradi konkatenciju stringova i izbaci gresku "Tekst.exe has stopped working"
|
|
|
|
Poslao: 02 Dec 2013 17:52
|
offline
- morando

- Građanin
- Pridružio: 19 Maj 2011
- Poruke: 297
|
Evo ti ceo kod ispravljen:
- #include<iostream>
- #include<string.h>
- #include<stdlib.h>
-
- using namespace std;
-
- class Tekst
- {
- friend ostream &operator<<(ostream &,const Tekst &);
- friend istream &operator>>(istream &,Tekst &);
- public:
- Tekst();
- Tekst(const char *);
- Tekst(const Tekst &);
- Tekst &operator=(const Tekst &t);
- Tekst operator+(const Tekst &t);
- Tekst &operator+=(const Tekst &t);
- ~Tekst();
- private:
- char *txt;
- };
- ostream &operator<<(ostream &out,const Tekst &t)
- {
- out<<t.txt; return out;
- }
-
- istream &operator>>(istream &in,Tekst &t)
- {
- in>>t.txt; return in;
- }
-
- Tekst::Tekst()
- {
- txt= 0;//new char(0);
- }
-
- Tekst::Tekst(const char *t)
- {
- txt=new char[strlen(t)+1];
- strcpy(txt,t);
- }
-
- Tekst::Tekst(const Tekst &t)
- {
- txt=new char[strlen(t.txt)+1];
- strcpy(txt,t.txt);
- }
-
- Tekst &Tekst::operator=(const Tekst &t)
- {
- if(this!=&t)
- {
- delete [] txt;
- txt=new char[strlen(t.txt)+1];
- strcpy(txt,t.txt);
- }
- return *this;
- }
- Tekst Tekst::operator+(const Tekst& t)
- {
- Tekst k;
- k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(k.txt, txt);
- strcat(k.txt, t.txt);
- return k;
- }
- Tekst &Tekst::operator+=(const Tekst& t)
- {
- char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
- strcpy(temp, txt);
- delete [] txt;
- strcat(temp, t.txt);
- txt = temp;
- return *this;
- }
-
- Tekst::~Tekst()
- { delete [] txt; txt = 0; }
-
- int main()
- {
- int i=0;
- Tekst a,b("Beograd"),c(b),d,f;
- //cout<<"a: "; cout<<a<<endl;
- cout<<"b: "; cout<<b<<endl;
- cout<<"c: "; cout<<c<<endl;
- d=b;
- cout<<"d: "; cout<<d<<endl;
- c+=d;
- cout<<"Prva konkatenacija: "<<c;
- f=c+d;
- cout<<"\nDruga konkatenacija: "<<f;
-
- return 0;
- }
Imao si gresku u operator = , txt treba da bude pokazivac na niz karaktera a ti si ga alocirao tamo kao pokazivac na 1 char:
- txt=new char(strlen(t.txt)+1);
Takodje sam izmenio default konstruktor jer ne mora nista da alocira.
|
|
|
|