Idesh
Idesh

Reputation: 373

Error while using Assignment operator overloading in c++ to copy data of a class object to another class object

I'm trying to copy values of one class object to another class object, But assignment operator overloading method is not working.

class rectangle
{
    int length,breadth;
public:
    rectangle(int l,int b)
    {
        length=l;
        breadth=b;
    }
   rectangle operator =(square s) //This line is giving me error.
    {
        breadth=length=s.seee();
        cout<<"length"<<length;
    }
    int see() const
    {
        return length;
    }
};
class square
{
    int side;
public:
    square()
    {
        side=5;
    }
    square operator =(rectangle r)
    {
        side=r.see();
        cout<<side;
    }
    int seee() const
    {
    return side;
    }
};

Error= 's' has incomplete type. How can I resolve this error? Please help!

Upvotes: 0

Views: 132

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117258

You need to do the implementation of the member function after you've defined square. Also note that the assignment operators are expected to return a reference to the object being assigned to, this, and that the right hand side of the operation (the square in this case) is usually taken as a const& to avoid unecessary copying.

class rectangle
{
//...
    rectangle& operator=(const square&);
//...
};

class square
{
//...
};

rectangle& rectangle::operator=(const square& s)
{
    breadth=length=s.seee();
    return *this;
}

Upvotes: 2

Related Questions