0

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!

0

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.