1

I am trying to use overloading concept to equate 3 objects c1, c2, c3. But it is giving me an error

error: no match for 'operator=' in 'c3 = c2. circle::operator=(((circle&)(& c1)))'

What's the reason behind it how do I rectify it??

#include<iostream>
using namespace std;

class circle
{
  private:
    int radius;
    float x,y;
  public:
    circle()
    {}
    circle(int rr,float xx,float yy)
    {
      radius=rr;
      x=xx;
      y=yy;
    }
    circle& operator=(const circle& c)
    {
     cout<<endl<<"assignment operator invoked";  
     radius=c.radius;
     x=c.x;
     y=c.y;
     return *this;
     }
    void showdata()
    {
      cout<<endl<<"\n radius="<<radius;
      cout<<endl<<"x coordinate="<<x;
      cout<<endl<<"y coordinate="<<y<<endl;
    }
};
int main()
{
  circle c1 (10,2.5,2.5);
  circle c2,c3;
  c3=c2=c1;
  c1.showdata();
  c2.showdata();
  c3.showdata();
  return 0;
} 

so this overloaded operator will be called two times.. First for c2=c1 and then for c3=c2 but how will compiler compare it with overloaded operator definition??

amadeus
  • 69
  • 4

1 Answers1

6

In order to chain operator= calls, you must make sure that it returns a reference

circle& operator=(const circle& c)
{
   cout<<endl<<"assignment operator invoked";  
   radius=c.radius;
   x=c.x;
   y=c.y;
   return *this;
}

c1=c2=c3 is parsed as c1 = (c2 = c3). If operator = doesn't return a reference, c2 = c3 is an rvalue, and it cannot bind to the reference argument of c1.operator = (In case if the argument is a reference to const, it can bind to rvalues, but that doesn't mean you shouldn't return a reference).

Also note that it makes sense to take the parameter by const reference, because you don't want to change the argument that you assign.

Also remember the rule of three, that is, if you really need to do any of the following:

  • overload operator =

  • explicitly provide a copy constructor

  • explicitly provide a destructor

then you probably want to do the other two too. In your particular case, you don't seem to need to overload operator = at all.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I'd add a mention of taking a `circle const&` too. – R. Martinho Fernandes Jul 13 '12 at 08:16
  • @ArmenTsirunyan Your snide remarks don't help anyone. And if you notice, I'm talking about the class he passes in. – Nathan White Jul 13 '12 at 08:23
  • @nathanwhite: Access checking is done on class level, not object level. And my remark wasn't meant to be snide. – Armen Tsirunyan Jul 13 '12 at 08:24
  • 1
    @ArmenTsirunyan Normally, constructive comments aren't followed by "duh". That is fine, and as it turns out, I am in the wrong anyway. I must admit (and this makes me sound like an incompetent programmer) that I didn't know that was possible. – Nathan White Jul 13 '12 at 08:28
  • @ArmenTsirunyan dt means above code wont work wo making correction that u specified..?? – amadeus Jul 13 '12 at 08:29
  • 1
    @amadeus: It might work, but it makes absolutely no sense. Why would you want to return a new object, identical to `*this` if you already have `*this`? The idiomatic way of implementing `opertator=` is to `return *this` – Armen Tsirunyan Jul 13 '12 at 08:36
  • 1
    @amadeus: Then I'd recommend to study with a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I personally recommend [Lippman's C++ Primer](http://www.amazon.com/dp/0201721481/?tag=stackoverfl08-20). – Armen Tsirunyan Jul 13 '12 at 08:50
  • 1
    Don't feel too bad, @ArmenTsirunyan; I was in a discussion a month or so ago with very experienced programmers, and the fact that any object can access the guts of all other objects of that same class surprised a couple of them. It's something they just never thought about. But without that feature, you couldn't implement copying constructors. Or just about anything interesting (`operator+` that adds private data from both objects, for instance). – Max Lybbert Jul 13 '12 at 08:52
  • 1
    @MaxLybbert: You probably meant to tag nathanwhite, not me :) – Armen Tsirunyan Jul 13 '12 at 08:53
  • @ArmenTsirunyan :thnx..i l refer to it..in meantime can u tell me that if this overloaded operator will be called two times.. First for c2=c1 and then for c3=c2 but how will compiler c2=c1 compare it with overloaded operator definition??Out of c2 and ca in c2=c1 call whose private fields are accessed and to which object value is returned..?? – amadeus Jul 13 '12 at 08:59
  • Sorry, Armen, you are right, I should have tagged @nathanwhite. – Max Lybbert Jul 13 '12 at 17:15
  • 1
    @MaxLybbert I appreciate that, and it does make me feel better haha! I must admit, I haven't worked with C++ in quite a while, so I'm not suprised that one caught me out. Thank you. – Nathan White Jul 13 '12 at 20:15