2

I have class Item and Item items and int q. I'm trying to make a function to overload+=. Would I need a friend function, or does it have to be member?

The statement in main program is

             items+=q;

in class Item header file:

   friend Item operator+=(const Item&, int&);

in class Item cpp file:

    Item operator+=(const Item& items, int& q)
    {
         items+=q;
         return items;
    }

And so compiler says no match for "+="

kfsone
  • 23,617
  • 2
  • 42
  • 74
Alex
  • 119
  • 3
  • 9
  • 2
    see [here](http://stackoverflow.com/questions/4421706/operator-overloading) – GreatAndPowerfulOz Jul 19 '16 at 20:58
  • 2
    Your `fiend Item operator+=(const Item&, int&)` can't work: `item` is what is going to have `q` added to it, but your function takes a `const` reference, which means it can't be modified. `operator+=(item&, const int&)` would work. – kfsone Jul 19 '16 at 21:12
  • Also, the function is infinitely recursive. `+=(Item,int)` unconditionally calls `+=(Item,int)` – Mooing Duck Jul 20 '16 at 00:06

3 Answers3

1

Both possibilities are possible.

http://en.cppreference.com/w/cpp/language/operators http://en.cppreference.com/w/cpp/language/operator_assignment (see the table)

Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
1

A += operator may either return void like this:

class Point
{
private:
  int x;
  int y;
public:
  void operator += (const Point& operand)
  {
     this->x += operand.x;
     this->y += operand.y;
  }
};

or may return a reference:

class Point
{
private:
  int x;
  int y;
public:
  Point& operator += (const Point& operand)
  {
     this->x += operand.x;
     this->y += operand.y;

     return *this;
  }
};

The latter is the better way to do things as it allows chaining.

Zulukas
  • 1,180
  • 1
  • 15
  • 35
  • It's not working because int q is in my main program, and I'm trying to do items+=quantity in my Item class definition. quantity is a private member of class Item. It won't compile because it says it can't convert invert 'int' to class 'Item'. – Alex Jul 19 '16 at 20:38
  • No, assignment operators typically return reference to itself, so you can chain assignments. – Slava Jul 19 '16 at 20:53
  • Fair enough, been a while since I've had a need to write overloaded operator like this. – Zulukas Jul 19 '16 at 20:57
  • Jane, I'm seeing a few things that are going to cause issues. First of all, you're attempting to modify a constant reference of an Item object. That won't work as you're disregarding the const keyword. Second, you don't need to pass in an Item object to this operator because it's going to modify the object itself. Third, as Slava pointed out, make sure you're returning a reference of the object. You may use 'return *this;'. – Zulukas Jul 19 '16 at 21:04
  • If I don't pass the "int" object to the function, it gives me a compilation error though. – Alex Jul 19 '16 at 21:23
0

change this:

Item operator+=(const Item& items, int& q)
    {
         items+=q;
         return items;
    }

to

Item operator+=(int q)
    {
         this->quanity += q;
         return *this;
    }

of course, this implies it's a member of the class...

GreatAndPowerfulOz
  • 1,767
  • 13
  • 19