Thread: Class assignment operator

  1. #1
    Unregistered
    Guest

    Question Class assignment operator

    hi all
    Can any one please help me out with this error msg that I am getting.
    The error msg is
    ----------------------------------------------
    binary '=' : no operator defined which takes a right-hand operand of type 'class Basket *' (or there is no acceptable conversion)
    ------------------------------------------------

    what I am trying to do is,

    Basket apple1;
    Basket apple2;
    Basket apple3;

    apple3 = apple1 * apple2;

    I already over loaded the operator*, operator<< ... I am not sure where I am going wrong. I can see that it is the liking the assignment operator but I am not sure how to proceed with that.
    In my "operator*", it takes the left hand side (lhs) and multiplies with the right hand side (rhs) and then returns a pointer to the new Basket class where I store the answer...
    Now logically I am thinking that when I do a = b*c it should take the b*c and return a pointer to the answer class which will be a. Please help me out thank you...

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    you're on the right track

    i see you understand a little about operator overloading when it comes to "user" defined object.......

    however, when you want to assign one user-defined object to another you also have to overload the assigment operator ........operator=........
    based on the same process you've been using, meaning utilize the passed in paramater object (rhs) to perform a so called deep copy......so for instance when a


    apple3 calls a ......operator=() ......function's body should copy whatever members you designate to be copied.....

    also you can overload the "copy constructor" .........


    hope this helps.....

    Regards,
    matheo917

  3. #3
    Unregistered
    Guest

    overload(brain)

    Thank you very much matheo917
    I see what you saying.. But here is the issue that I am not understanding. When I trying to overload operator=(), I am only passing one object by reference. because when I write a statement like this " a = b * c" the compiler will automatically first evaluate the b * c first and then pass the result of b*C as one basket class to "a". But for some reason my compiler is complaining when I do that.
    Funny thing is when I write the statement:
    apple1 = apple2; // i get no error.
    when I do "apple1 * apple2;" // I also dont get any error
    but when I do "apple3 = apple1 * apple2 //it complains and outputs the same error msg...
    Now if it makes any difference I am using stl<list> to keep track of all my apples in the basket.
    Now I made apple its own class with only two paramets. size and color.
    for example
    class Apple
    {
    public:
    int size;
    ch color; //only two possible color r-Red g-Green
    }

    then in the basket class i have
    class basket
    {
    // overload all the operators
    list<Apple> applebasket;
    }

    I am not sure where I am going wrong. in my apple class or in my basket. It is higly unlikly that apple class is wrong. But I was thinking do I also need to overload the operators in my Apple class. Thank you in advance for all the help.

  4. #4
    Unregistered
    Guest

    Any one

    How do you write a copy constructor for the basket class?

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    To write a copy constructor, you have the constructor take an instance of the whatever class as a paramter and assign whatever is in it to the new instance.

  6. #6
    Unregistered
    Guest
    for example
    class Apple
    {
    public:
    int size;
    ch color; //only two possible color r-Red g-Green
    };

    since you didn't provide a constructor for the Apple class posted above, the compiler provided a default default constructor and default copy constructor for you. It also provides a default destructor and default assignment operator if you don't provide one. Therefore:

    apple1 = apple2;

    should proceed normally using the default assignment operator the compiler provided; and, as there is no pointer or dynamic memory involved in the Apple class, it is probably adequate. However:

    apple1 * apple2;

    should provoke an error message from your compiler as it has no knowledge of how to multiple two Apples together since the * operator is not overloaded. The error you are getting in:

    apple3 = apple1 * apple2;

    isn't the one you expect as the compiler is interpretting apple1 * to be a pointer of type apple1 and is trying to assign the pointer of type apple1 to apple3, but it can't find an appropriately overloaded assignment operator to do so.

    Trying to overload the * operator such that it multiplies one basket class by another will require you to be very adroit as it is not a standard notion as to what is meant by multiplying one list by another.

  7. #7
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i think that my predececor is on the right track, however if it comes to me i would have to see more code, perhaps the whole source code in order to be fully able to utilize my knowledge and experience......

    Regards,
    matheo917

  8. #8
    Unregistered
    Guest

    Thank you all

    Hey I like to thank you all for helping me out. I finally got it. What happened is I overloaded the operator* as a pointer. Just so it would be easy for me to get access to the private data in the apple class. Anyways when I declear a Basket class, I needed to declear it as a pointer also. It was not able to convert the *class to regular class. I also had to overload the operator*=(). So thankyou all again for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy Assignment Operator Semantics
    By SevenThunders in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 01:08 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Replies: 3
    Last Post: 05-23-2007, 06:15 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM