Operator Overload = Not Working

This is a discussion on Operator Overload = Not Working within the C++ Programming forums, part of the General Programming Boards category; Again Problem With teh Same Point Class Code Pasted Here http://phpfi.com/246031 BEFORE I used Code: pt2 = pt1;//Works fine But ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    Operator Overload = Not Working

    Again Problem With teh Same Point Class
    Code Pasted Here http://phpfi.com/246031
    BEFORE I used
    Code:
    pt2 = pt1;//Works fine
    But I noticed that
    Its not calling the overloaded 'Operator='
    So I made it
    Code:
    *pt1 -= *pt2;
    On line 44 But Although Its Calling the Overloaded 'Operator='
    {As its printing "I am Getting Executed" on the line}
    This is the 'Operator=' function
    Code:
    Point* Point::operator=(const Point& pt){
    	cout<<"I am Getting Executed"<<endl;
    	return this;//datatype Of this is Point* Const
    }
    On line 20-22
    Please help.
    --------------------------------------------------------------------
    here is the Output of this Programm
    --------------------------------------------------------------------
    Code:
    Describing Class :              pt1             x = 15  y = 10
    Describing Class : pt2 before Copying           x = 4   y = 8
    I am Getting Executed
    Describing Class : pt2 after Copying            x = 4   y = 8
    destructing
    Describing Class : pt2 after Increamenting              x = 9   y = 13
    destructing
    Describing Class : pt1 after Decrementing               x = 6   y = -3
    Last edited by noobcpp; 06-29-2007 at 10:56 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Make operator=, operator+= and operator-= all return a Point&, not a Point or Point*.

    In fact, because none of your member variables are in need of memory management by you, you can just use the compiler provided copy assignment operator and thus not provide your own operator=.

    Also, as mentioned, there is no need to write:
    Point* pt1 = new Point(15, 10);
    You can just use:
    Point pt1(15, 10);

    If you do use new, then you should have a corresponding delete.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks anyway
    I've fixed it by myself.
    My 'operator=' Function
    Code:
    Point* Point::operator=(const Point& pt){
    	return this;//datatype Of this is Point* Const
    }
    was not doing anything So I made it like this.
    Code:
    Point* Point::operator=(const Point& pt){
    	this->x = pt.x;
    	this->y = pt.y;
    	return this;//datatype Of this is Point* Const
    }
    And Its Working Fine
    I am New to C++ Previously I used Code like this
    var = new class_name(args)
    var->member_function()->member_var
    So I need to use new to adjust.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    That is a wrong fix. Yes, this is a pointer. But now, how would you go about doing copying without pointers?

    Rather, write your copy assignment operator along these lines:
    Code:
    Point& Point::operator=(const Point& pt) {
        x = pt.x;
        y = pt.y;
        return *this;
    }
    You may also include a check for self-assignment:
    Code:
    Point& Point::operator=(const Point& pt) {
        if (this != &pt) {
            x = pt.x;
            y = pt.y;
        }
        return *this;
    }
    If you provide a swap member function, you could even write:
    Code:
    Point& Point::operator=(const Point& pt) {
        Point temp(pt);
        swap(temp);
        return *this;
    }
    I am New to C++ Previously I used Code like this
    var = new class_name(args)
    var->member_function()->member_var
    So I need to use new to adjust.
    Then where's your delete? I suggest that you learn how to use C++ without pointers first. It seems to be causing you lots of unnecessary problems.
    Last edited by laserlight; 06-29-2007 at 11:52 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote:I am New to C++ Previously I used Code like this
    var = new class_name(args)
    var->member_function()->member_var
    So I need to use new to adjust.

    Then where's your delete?
    I didnt need to delete often cause after execution of the Function it went out from memory.
    Whenm used As Global I used unset(var) to delete.But that was not mandatory.
    ------------------------------------------------------------------
    However I didnt Understand this Line
    how would you go about doing copying without pointers? It would be something of a hack involving the address-of operator,

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I didnt need to delete often cause after execution of the Function it went out from memory.
    Whenm used As Global I used unset(var) to delete.But that was not mandatory.
    What's unset()? As for delete: there is no guarantee that the system will reclaim resources without the delete, so just use it.

    However I didnt Understand this Line
    Actually, I think that by returning a Point*, you probably cannot use it for normal copy assignment.

    Take this for example:
    Code:
    Point a, b(1, 2);
    a = b;
    At a glance, most people will expect that this means that a is now a copy of b. With your code, there would be an error. So, how do we make a into a copy of b?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So here I need To use
    Code:
    Point* pt1 = new Point;
    Point *pt2 = new Point(1, 2);
    *a = *b
    am I right ??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    hmm... I looked more carefully, and I think that I am wrong: there should not be an error produced for my example. There would only be an error for operator chaining, e.g.,
    a = b = c;

    So here I need To use
    No, stop using pointers for the time being. You do not need them. It should be something as simple as:
    Code:
    Point pt1;
    Point pt2(1, 2);
    a = b;
    Last edited by laserlight; 06-30-2007 at 12:20 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ok this is my code now http://phpfi.com/246046
    I am Getting this Following Error

    no Match for 'operator=' in 'pt3 = pt1'
    No candidats are : Point* point::operator=(const Point&)

    NOTE
    NO problem on line Number 41 it has been fixed before
    I've forgotten to remove that Comment line
    Last edited by noobcpp; 06-30-2007 at 12:33 AM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Why are you using pointers? Why are you using new? This is not Java, you don't use new to create normal objects.

    You also aren't using the advice you are being given. You don't need an operator=, and if you are going to write one don't have it return a Point*.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I am not making it professionaly I need to learn Pointers, I need to focus on new . I am Learning C++
    And see on line 45 I am Using normal Class
    and then I got that Error
    Please tell me how can I solve the Problem.

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Cool just one Change and it worked
    I've changed on line number 48
    pt3 = pt1;
    to
    pt3 = *pt1;
    It worked Nice
    But Why ??

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by noobcpp View Post
    Cool just one Change and it worked
    I've changed on line number 48
    pt3 = pt1;
    to
    pt3 = *pt1;
    It worked Nice
    But Why ??
    It didn't work because you were assigning a pointer to an object.
    Now you're assigning an object to an object.
    Trouble is that the compiler is not using your overloaded assignement operator
    Code:
    Point * operator=(const Point&);//Overloading = operator
    it uses the the
    Code:
    Point & operator=(const Point&);
    that it created for you and this will result in a memory leak
    because of the char* note member variable.
    Kurt

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because pt1 is a pointer and needs to be dereferenced like you do in every other place...

    Trouble is that the compiler is not using your overloaded assignement operator...
    Why would that be? If it were so, wouldn't there be an error because there's a function overload based on return type only?

    Returning a pointer from operator= is bad, as mentioned earlier, but in x = y; (x.operator=(y);) the return value is not used.
    Last edited by anon; 06-30-2007 at 05:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    No Compiler is executing My overloaded operator I've tested using
    Code:
    Point* Point::operator=(const Point& pt){
    	cout<<"Compiler is executing Me"<<endl;
    	this->x = pt.x;
    	this->y = pt.y;
    	return this;//datatype Of this is Point* Const
    }
    And it has Printed "Compiler is executing Me" 2 times
    I've forgotten to Use the char* note I'll Use that latter.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21