2017-06-13 4 views
-2

Ich war für die Universität Codierung und ich steckte in einer Aufgabe.Es sagt, "Fehler: nicht übereinstimmen 'Operator' =", aber ich habe das '=' überlastet, so konnte ich das Problem nicht finden.Kann jemand helfen ich raus? DankFehler: Keine Übereinstimmung für 'operator =' (Operandentypen sind 'PlDrustvo' und 'PlDrustvo') |

Der Code ist:

#include <iostream> 
#include <cstring> 
using namespace std; 

class PlDrustvo{ 
private: 
    char *ime; 
    int turi; 
    int brclenovi; 
public: 
    PlDrustvo(){ ime = new char[1]; 
     strcpy(ime, " "); 
     turi = 0; 
     brclenovi = 0;} 
    PlDrustvo(char *ime, int turi, int brclenovi){ 
    this->ime=new char[strlen(ime)+1]; 
    strcpy(this->ime,ime); 
    this->turi=turi; 
    this->brclenovi=brclenovi; 
    } 
    PlDrustvo(PlDrustvo &p){ 
    this->ime=new char[strlen(p.ime)+1]; 
    strcpy(this->ime,p.ime); 
    this->turi=p.turi; 
    this->brclenovi=p.brclenovi; 
    } 
    PlDrustvo operator+(PlDrustvo &c){ 
    PlDrustvo temp; 
    temp.brclenovi=this->brclenovi+ c.brclenovi; 
    if(this->brclenovi>c.brclenovi){ 
     temp.ime=new char[strlen(this->ime)+1]; 
     strcpy(temp.ime,this->ime); 
     temp.turi=this->turi; 
     } 
    else 
    { 
    temp.ime=new char[strlen(c.ime)+1]; 
    strcpy(temp.ime,c.ime); 
    temp.turi=c.turi; 
    } 
    return temp; 
    } 
    PlDrustvo& operator=(PlDrustvo &p){ 
    if(this!=&p) 
    { 
     delete [] this->ime; 
     this->ime=new char[strlen(p.ime)+1]; 
    strcpy(this->ime,p.ime); 
    this->turi=p.turi; 
    this->brclenovi=p.brclenovi; 
    } 
    return *this; 

    } 
bool operator>(PlDrustvo &p){ 
return this->brclenovi>p.brclenovi; 
} 
bool operator<(PlDrustvo &p){ 
return brclenovi<p.brclenovi; 
} 
friend ostream& operator<<(ostream &alek, PlDrustvo &p){ 
alek<<"Ime: "<<p.ime<<" Turi: "<<p.turi<<" Clenovi: "<<p.brclenovi<<endl; 
return alek; 
} 
friend void najmnoguClenovi(PlDrustvo*, int); 
}; 
void najmnoguClenovi(PlDrustvo *pl,int n){ 
int i_max = 0; 
    for(int i=0; i<n; i++) 
     if(pl[i].brclenovi > pl[i_max].brclenovi) 
      i_max = i; 
    cout << "Najmnogu clenovi ima planinarskoto drustvo: " << pl[i_max]; 
} 
int main() 
{ 
    PlDrustvo drustva[3]; 
    PlDrustvo pl; 
    for (int i=0;i<3;i++) 
    { 
     char ime[100]; 
     int brTuri; 
     int brClenovi; 
     cin>>ime; 
     cin>>brTuri; 
     cin>>brClenovi; 
     PlDrustvo p(ime,brTuri,brClenovi); 
     drustva[i] = p; 

    } 

    pl = drustva[0] + drustva[1]; //HERE IS THE ERROR 
    cout<<pl; 

    najmnoguClenovi(drustva, 3); 

    return 0; 
} 
+0

Hinweis: Immer in Englisch programmieren. Und Einzug besser. –

+0

Verwenden Sie std :: string! Sie können nicht "" in ein Array der Größe 1 streichen! –

+0

_i konnte das Problem nicht finden, weil es dort 100 Zeilen Code gibt. Reduziere es auf [mcve] und du wirst das Problem sehen (und anderen helfen) – Tas

Antwort

1
PlDrustvo& operator=(PlDrustvo &p){ 

PlDrustvo& operator=(PlDrustvo const &p){ 

Die genaue Fehlermeldung werden sollten:

error: cannot bind non-const lvalue reference of type 'PlDrustvo&' to an rvalue of type 'PlDrustvo'

Der Rückgabewert der Summierungsoperation ist ein temporärer Wert vom Typ PlDrustvo, und Provisorien können nicht an nicht-konstante Referenzen binden, sodass der Compiler das Ergebnis des Summationsausdrucks nicht an Ihren Kopierzuweisungsoperator übergeben kann. Die Lösung besteht darin, operator= stattdessen eine const-Referenz zu akzeptieren, was ohnehin sinnvoller ist, da das Argument nicht geändert wird.

Mit dieser Änderung, your code compiles.


Im Allgemeinen leidet Ihr Programm an einem Mangel an const-Korrektheit. Hier sind alle anderen Probleme, die ich gefunden habe:

/* bad */ PlDrustvo(char *ime, int turi, int brclenovi){ 
/* good */ PlDrustvo(char const *ime, int turi, int brclenovi){ 

/* bad */ PlDrustvo(PlDrustvo &p){ 
/* good */ PlDrustvo(PlDrustvo const &p){ 

/* bad */ PlDrustvo operator+(PlDrustvo &c) { 
/* good */ PlDrustvo operator+(PlDrustvo const &c) const { 

/* bad */ bool operator>(PlDrustvo &p){ 
/* good */ bool operator>(PlDrustvo const &p){ 

/* bad */ bool operator<(PlDrustvo &p){ 
/* good */ bool operator<(PlDrustvo const &p){ 

/* bad */ friend ostream& operator<<(ostream &alek, PlDrustvo &p){ 
/* good */ friend ostream& operator<<(ostream &alek, PlDrustvo const &p){ 

/* bad */ friend void najmnoguClenovi(PlDrustvo*, int); 
/* good */ friend void najmnoguClenovi(PlDrustvo const*, int); 

/* bad */ void najmnoguClenovi(PlDrustvo *pl,int n){ 
/* good */ void najmnoguClenovi(PlDrustvo const *pl,int n){ 
Verwandte Themen