错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
error: no match for 'operator=' (operand types are 'PlDrustvo' and '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;
}
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){
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|