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;
}
Hinweis: Immer in Englisch programmieren. Und Einzug besser. –
Verwenden Sie std :: string! Sie können nicht "" in ein Array der Größe 1 streichen! –
_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