2017-06-13 65 views
-2

我正在爲大學編碼,我卡在一個作業。它說, 「錯誤:'不匹配'操作員'=」,但我已經超載'=',所以我找不到問題。可以幫助某人我出去了? 由於錯誤:'operator ='不匹配(操作數類型是'PlDrustvo'和'PlDrustvo')|

的代碼是:

#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

提示:總是用英語編程。並縮進更好。 –

+0

使用std :: string!你不能strcpy「」成一個大小爲1的數組! – 2017-06-13 22:03:39

+0

_i無法找到問題,這是因爲那裏有100行代碼。將其降低到[mcve],您可能會看到問題(以及幫助其他人提供幫助) – Tas

回答

1
PlDrustvo& operator=(PlDrustvo &p){ 

應該是

PlDrustvo& operator=(PlDrustvo const &p){ 

確切的錯誤信息是:

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

求和操作的返回值是臨時類型PlDrustvo,並且臨時對象不能綁定到非常量引用,因此編譯器無法將求和表達式的結果傳遞給您的複製分配操作符。解決的辦法是使operator=接受一個const引用,反正它更有意義,因爲它不會修改參數。

隨着這種變化,your code compiles


一般而言,您的程序缺乏const正確性。以下是我發現的所有其他常見問題:

/* 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){ 
相關問題