Overloading ++ Operator

This is a discussion on Overloading ++ Operator within the C++ Programming forums, part of the General Programming Boards category; Here is my simple Code http://phpfi.com/246184 I am trying to overload teh ++ operator I am getting this Following error ...

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

    Overloading ++ Operator

    Here is my simple Code
    http://phpfi.com/246184 I am trying to overload teh ++ operator
    I am getting this Following error
    Code:
    root@mPC:~/desktop# g++ -o op_overload op_overload.cpp
    op_overload.cpp:15: error: postfix ‘Point Point::operator++(const Point&)’ must take ‘int’ as its argument
    op_overload.cpp:22: error: postfix ‘Point Point::operator++(const Point&)’ must take ‘int’ as its argument
    op_overload.cpp: In member function ‘Point Point::operator++(const Point&)’:
    op_overload.cpp:23: error: increment of data-member ‘Point::x’ in read-only structure
    op_overload.cpp:24: error: increment of data-member ‘Point::y’ in read-only structure
    can anyone help me please ??

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    58
    Code:
    Point Point::operator++(const Point& pt){
    Try taking the const out.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Its all Same

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    I believe that it would be more common to write it as:
    Code:
    Point& Point::operator++()
    {
        ++x;
        ++y;
        return *this;
    }
    You might want to read an FAQ on operator overloading.
    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
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    p_overload.cpp:15: error: postfix ‘Point Point::operator++(const Point&)’ must take ‘int’ as its argument
    The compiler thinks you want to create the postfix ++ operator.
    that has a funny signature, you have to give it a dummy int parameter
    Code:
    Point & Point::operator++(int)
    laserlights example creates the prefix ++ operator
    Kurt

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So what would be the Prototype ??
    Code:
    Point operator++();

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks Worked
    Code:
    #include <iostream>
    //using simple Overloading ON Simple Objects Without New
    using namespace std;
    class Point{
    	private:
    		int x, y;
    	public:
    		Point(){}//default Constructor
    		Point(int, int);//Constructor
    		Point(const Point&);//Copy Constructor
    		void desc(const char* txt){
    			cout<<"describing "<<txt<<endl;
    			cout<<"x = "<<this->x<<"\t"<<"y = "<<this->y<<endl;
    		}
    		Point& operator++();//The Operator Overloader Prototype
    		~Point(){cout<<"destructing"<<endl;}
    };
    Point::Point(int a, int b){
    	this->x = a;
    	this->y = b;
    }
    //operator Overloader function declearation
    Point& Point::operator++(){
    	++x;
    	++y;
    	return *this;
    }
    int main(){
    	Point pt(4, 10);
    	pt.desc("pt");
    	return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    So what would be the Prototype ??
    If you read the material at the link I posted, you would see that the suggested prototype for the prefix operator++ is:
    Code:
    Point& operator++();
    And the suggested prototypes (pick one) for the postfix operator++ are:
    Code:
    Point operator++(int);
    // or
    void operator++(int);
    Note that in all cases they are member functions.
    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
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by noobcpp View Post
    So what would be the Prototype ??
    Code:
    Point operator++();
    prefix
    Code:
    Point & operator++();
    postfix
    Code:
    Point & operator++(int);
    and it should return a reference, it doesnt make much sense to return an object from that operators.
    Kurt

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    and it should return a reference, it doesnt make much sense to return an object from that operators.
    Not for the postfix operator++ though. Typically one would create a temporary of the original value, increment the original value, and then return the temporary. If one returned a reference, then it would a reference to an object that is now out of scope.

    Come to think of it, the result of a postfix operator++ for primitive types should be an rvalue, so perhaps it would be even better to write:
    Code:
    const Point operator++(int);
    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

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by laserlight View Post
    Not for the postfix operator++ though. Typically one would create a temporary of the original value, increment the original value, and then return the temporary. If one returned a reference, then it would a reference to an object that is now out of scope.
    True, I should think a little bit more before replying.
    Kurt

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    "Do as the ints do."
    Scott Meyers recommends this strategy for ++:
    Code:
    class Foo
    {
    public:
      Foo& operator ++();
      const Foo operator ++(int);
    };
    
    Foo &Foo::operator ++()
    {
      // class-specific increment logic
      return *this;
    }
    
    const Foo Foo::operator ++(int)
    {
      Foo t(*this);
      ++*this;
      return t;
    }
    See Item 6 of More Effective C++ for the full reasoning.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Why int is required as an argument for Postfix ??

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    They just needed some way to distinguish between prefix and postfix, so since doing that won't harm anything else they decided it would be the best option.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    What we are doing is
    Code:
    Point& Point::operator++(){
    	++x;
    	++y;
    	return *this;
    }
    here & of *this means Reference of Pointer Means teh Object So It should also work
    Code:
    Point Point::operator++(){
    	++x;
    	++y;
    	return this;
    }
    But Its not working. Why??
    ya I've also changed teh Prototype to This
    Code:
    Point operator++();
    But Still not working. Why ??

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

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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