2

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);
                             ^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Tim Dirks
  • 439
  • 1
  • 6
  • 17
  • Obviously you cannot change the value of a const float. See how to properly overload operators here http://stackoverflow.com/questions/4421706/operator-overloading – aslg Aug 16 '15 at 15:02

1 Answers1

4

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks that solved it I think ( no errors) only need to test it. I updated my question because now I have an error wich I had before aswell and fixed with the constant. Have a hint for me aswell? – Tim Dirks Aug 16 '15 at 15:29
  • Thanks, last question than it should work. How to use the operator as I use it like you suggested? Animal & operator +=( Animal &animal, float amount); Because I wanted to do weight+=amount; Because I think I can't access the animal right? – Tim Dirks Aug 16 '15 at 15:43
  • @Tim Dirks It is the first parameter that is changed. For example animal.setWeight( animal.getWeight() + amount ); return animal; – Vlad from Moscow Aug 16 '15 at 15:47
  • Yes oke, but how to come in the operator function? Or how to use the += operator then in the feed function? – Tim Dirks Aug 16 '15 at 16:12
  • @Tim Dirks It is evident that the function should be declared without quakifier const because it change the object. within the function you can write for example either like *this += amount; or operaor +=( *this, amount ); But it would be better if the operator is declared as a member function – Vlad from Moscow Aug 16 '15 at 16:18