Zadatak c++, pomoc

Zadatak c++, pomoc

offline
  • 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.
offline
  • 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.



offline
  • 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. Smile

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; }

offline
  • 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.

offline
  • Pridružio: 18 Jan 2013
  • Poruke: 13

Uradio sam ja, al' sam zaboravio da azuriram odgovor Smile.
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; }

offline
  • 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)

offline
  • 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"

offline
  • 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.

Ko je trenutno na forumu
 

Ukupno su 1008 korisnika na forumu :: 56 registrovanih, 7 sakrivenih i 945 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: 357magnum, _Rade, arsa, babaroga, bojank, dane007, dankisha, DeerHunter, djboj, Djokkinen, Doca, doklevise, DonRumataEstorski, Dorcolac, dule10savic, GandorCC, gorican, havoc995, ikan, Još malo pa deda, Klecaviks, KOV, Krusarac, kunktator, kybonacci, ljuba, lord sir giga, LUDI, Luka Blažević, mercedesamg, Metanoja, mgolub, milenko crazy north, MiroslavD, Misirac, mnn2, mrav pesadinac, Nemanja.M, nemkea71, Neretva, oldtimer, pein, S2M, sap, sasa87, slonic_tonic, Stoilkovic, tubular, vathra, VJ, vladulns, voja64, Volkhov-M, Wrangler, yufighter, zlaya011