【问题标题】:how to fix no viable overloaded '=' in cpp如何修复 cpp 中没有可行的重载 \'=\'
【发布时间】:2023-01-29 16:33:40
【问题描述】:
class Gebot{
    string name;
  int betrag;
  public:
  Gebot(string name, int betrag = 100) : name{name} , betrag{betrag} {
    //ganze Zahl >0 und <=10.000.000
    if(name.size() ==0 || betrag <=0 || betrag > 10000000)
      throw runtime_error("illegal name or deposite");
  }
  bool selbe_bieterin(const Gebot& gebot) const{
    if(gebot.name == this->name)
      return true;
    return false;
  }
  bool operator==(const Gebot& gebot) const{
    name = "name";
    if(gebot.betrag == this->betrag)
      return true;
    return false;
  }
  bool operator<(const Gebot& gebot) const{
    if(gebot.betrag > this->betrag)
      return true;
    return false;
  }
  bool operator>=(int gebot) const{
    if(gebot <= this->betrag)
      return true;
    return false;
  }
  friend ostream& operator<<(ostream& o, const Gebot & gebot){
    //[Susi: 263 Euro]
        
    o<<"["<<gebot.name<<": "<<gebot.betrag<<" Euro]";
    return o;
  }
};

为什么我会遇到这个问题25:10:错误:没有可行的重载“=” 名字 = "名字";尝试将变量名称更改为“名称”时。如何修复它。提前致谢)。


【问题讨论】:

标签: c++ class operators


【解决方案1】:

这个方法是常量

bool operator==(const Gebot& gebot) const {
                                    ^^^^^
    name = "name"; <<-- changing the value of name
    if(gebot.betrag == this->betrag)
       return true;
    return false;

}

但是你试图改变name的值,所以你为方法写的代码不是const

解决方案似乎是删除name = "name"; 行,但尚不清楚为什么它在那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多