【问题标题】:Using += operator with float values将 += 运算符与浮点值一起使用
【发布时间】:2015-11-09 06:28:47
【问题描述】:

我正在尝试实现一个运算符函数来解决下一个错误:

error: assignment of member 'Animal::weight' in read-only object weight +=amount*(0.02f);

我的 Animal.cpp 函数如下所示:

void Animal::feed(float amount) const
  {
  if (type == "sheep"){
        amount=amount*(0.02f);
        weight+=amount;
  }else if (type == "cow"){
      weight +=amount*(0.05f);
  }else if (type == "pig"){
       weight +=amount*(0.1f);
  }
  return weight;
}

Animal.h(短版):

    class Animal
      {
      public:
        Animal(std::string aType, const char *anSex, float aWeight, QDateTime birthday);
        float getWeight() const {return weight;};

        void setWeight(float value) {weight = value;};
        float feed(float amount) const;
        void feedAnimal(float amount);
      private:
        float weight;
      };

float operator+=(const float &weight,const float &amount);

然后我实现了一个 += 运算符。

float operator+=(const float &weight,const float &amount);

这也包含在 .cpp 文件中:

 Animal & operator +=(Animal &animal, float amount){
    float w = animal.getWeight();
    animal.setWeight(w+amount);
    }

我使用了一个参考,以便为每只动物更新体重。所以我可以调用函数提要,当我想知道结果时,我会使用 get 函数:

float getWeight() const {return weight;};

但由于某种原因,我发现了下一个错误:

 'float operator+=(const float&, const float&)' must have an argument of class or enumerated type
 float operator+=(const float &weight,const float &amount);

有什么解决办法吗?

对于使用 feed 功能我也有问题。我有我的 Farm.cpp 类,我在其中循环查看农场中的所有动物。

void Farm::feedAllAnimals(float amount)
  {
  for (auto an : animals) {
          if(an != nullptr) {
                 an->feed(amount);
          }
   }
  std::cout << "all animals fed with " << amount << "kg of fodder";
  }

在我的 .h 文件中,我有这些功能:

Public:
    void feedAllAnimals(float amount);

Private: 
std::vector<std::shared_ptr<const Animal>> animals;

我的错误:

error: passing 'const Animal' as 'this' argument of 'float Animal::feed(float)' discards qualifiers [-fpermissive] an->feed(amount);
                             ^

【问题讨论】:

标签: c++ operator-overloading operators constants shared-ptr


【解决方案1】:

您将函数 feed 声明为 const 成员函数

void feed(float amount) const;
                        ^^^^^

如果它是一个常量对象,你不能改变它。

至于运营商

float operator+=(const float &weight,const float &amount);

那么你不能重载基本类型的操作符。 我认为您的意思是以下任一

Animal & operator +=( Animal &animal, float amount);

例如

Animal & operator +=( Animal &animal, float amount)
{
    animal.setWeight( animal.getWeight() + amount );

    return animal;
}

或在类中声明为成员函数的运算符,如

Animal & operator +=( float amount );

对于向量,如果要更改由 evctor 的元素指向的对象,则模板参数必须不带限定符 const

std::vector<std::shared_ptr<Animal>> animals;

【讨论】:

  • 谢谢解决它我认为(没有错误)只需要测试它。我更新了我的问题,因为现在我有一个错误,我以前也有过这个错误,并用常量修复了。也给我一个提示?
  • 谢谢,最后一个问题应该有效。如何像您建议的那样使用运算符? Animal & 运算符 +=( Animal &animal, 浮点数);因为我想做重量+=数量;因为我认为我无法接近动物吗?
  • @Tim Dirks 这是第一个被改变的参数。例如 animal.setWeight(animal.getWeight() + amount );返回动物;
  • 是的,但是如何进入算子函数呢?或者如何在 feed 函数中使用 += 运算符?
  • @Tim Dirks 很明显,应该在没有 quakifier const 的情况下声明该函数,因为它会更改对象。在函数中,您可以编写例如 *this += amount;或运算符 +=( *this, amount );但是如果将运算符声明为成员函数会更好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2020-04-22
  • 1970-01-01
  • 2017-11-17
  • 2013-09-10
  • 1970-01-01
相关资源
最近更新 更多