错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|

error: no match for 'operator=' (operand types are 'PlDrustvo' and 'PlDrustvo')|

本文关键字:PlDrustvo 类型 不匹配 operator 错误 操作数      更新时间:2023-10-16

我正在为大学编程,我坚持做一项作业。它说,"错误:与'运算符'="不匹配,但我重载了'=',所以我找不到问题。有人可以帮助我吗?谢谢

代码为:

#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;
}
PlDrustvo& operator=(PlDrustvo &p){

应该是

PlDrustvo& operator=(PlDrustvo const &p){

确切的错误消息是:

错误:无法将类型为"PlDrustvo&"的非常量左值引用绑定到类型为"PlDrustvo"的右值

求和操作的返回值是 PlDrustvo 类型的临时值,临时值不能绑定到非 const 引用,因此编译器无法将求和表达式的结果传递给复制赋值运算符。 解决方案是让operator=接受 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){