Search code examples
c++operator-overloadingoperatorsconstantsshared-ptr

Using += operator with float values


I'm trying to implement a operator function to solve the next error :

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

My Animal.cpp function looks like:

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 (short version):

    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);

Then I implemented a += operator.

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

Which is also included then in the .cpp file:

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

I worked with a reference so that the weight is update for every animal. So I can call the function feed, and when I want to know the result I do it with the get function:

float getWeight() const {return weight;};

But for some reason I catch the next error :

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

Any solutions for this?

For use the feed function I also have a problem. I have my Farm.cpp class where i loop for all the animals in the farm.

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";
  }

And in my .h file I have those functions :

Public:
    void feedAllAnimals(float amount);

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

My error:

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

Solution

  • You declared function feed as a const member function

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

    You may not change the object if it is a constant object.

    As for operator

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

    then you may not overload operators for fundamental types. I think you mean either the following

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

    For example

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

    or an operator declared as a member function within the class like

    Animal & operator +=( float amount );
    

    As for the vector then the template parameter must be without qualifier const if you are going to change objects pointed to by the elements of the evctor

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