overload same Operator twice

This is a discussion on overload same Operator twice within the C++ Programming forums, part of the General Programming Boards category; This is My overload functions for int Code: Point operator+=(int); Code: Point Point::operator+=(int inc){ this->x += inc; this->y += inc; ...

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

    overload same Operator twice

    This is My overload functions for int
    Code:
    Point operator+=(int);
    Code:
    Point Point::operator+=(int inc){
    	this->x += inc;
    	this->y += inc;
    	return *this;
    }
    Now I want to overload these functions to also work as
    Code:
    Point operator+=(const Point&);
    Can I do it Simply just as I did for int ??

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Try it, and see if it works.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Yes I've tried
    I've Pasted My Total Code on http://phpfi.com/246140
    On line 23 and 24
    Code:
    Point operator+=(int);
    Point operator+=(const Point&);
    Line 37-46
    Code:
    Point Point::operator+=(int inc){
    	this->x += inc;
    	this->y += inc;
    	return *this;
    }
    Point Point::operator+=(const Point pt){
    	this->x += pt.x;
    	this->y += pt.y;
    	return *this;
    }
    It fired Errors
    Code:
    oops_member_op_overload.cpp:42: error: prototype for ‘Point Point::operator+=(Point)’ does not match any in class ‘Point’
    oops_member_op_overload.cpp:24: error: candidates are: Point Point::operator+=(const Point&)
    oops_member_op_overload.cpp:37: error:                 Point Point::operator+=(int)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The errors tell you exactly what the problem is. The code on line 42 doesn't match any of the functions declared in your class. You have two operator+= functions declared in your class, but neither of them take a const Point. You have to make the definition match the declaration.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks I need to use
    Code:
    Point Point::operator+=(const Point& pt){
    	this->x += pt.x;
    	this->y += pt.y;
    	return *this;
    }
    here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 05:44 AM
  2. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 12:13 PM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. C++ Operator Overload Question
    By cworld in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2002, 07:17 PM
  5. Overload (+) operator
    By kit in forum C++ Programming
    Replies: 8
    Last Post: 10-23-2001, 12:20 PM

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