Thread: Overloading the >= operator

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Overloading the >= operator

    Here is my example
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Point3d {
    public:
        /**
      * Constructor with given value that will be set to all coordinates.
      * @param v - the value
      */
      Point3d( double v )
        : _x( v ), _y( v ), _z( v ) {};
        
      /**
      * Returns the first coordinate of this point (constant).
      * @return the \b first coordinate
      */
      const double x() const { return _x; };
    
    
      /**
      * Returns the second coordinate of this point (constant).
      * @return the \b second coordinate
      */
      const double y() const { return _y; };
    
    
      /**
      * Returns the third coordinate of this point (constant).
      * @return the \b third coordinate
      */
      const double z() const { return _z; };
      
        /**
      * Multiply all coordinates of this point with the given value.
      * @param w - the multiplier
      */
      void operator *= ( const double w ) {
        _x *= w; _y *= w; _z *= w;
      };
      
      
      bool operator >= ( const Point3d& p ) {
          return ( _x >= p.x()  && _y => p.y() && _z => p.z() );
      };
    private:
        double _x;
        double _y;
        double _z;
    };
    
    int main()
    {
        Point3d q(2);
        Point3d w(1);
        if(q>=w) cout<<"OK\n";
        return 0;
    }
    Why I get this error and how I fix it? I want to understand why this happens.
    Code:
    main.cpp: In member function `bool Point3d::operator>=(const Point3d&)':
    main.cpp:44: error: expected primary-expression before '>' token
    main.cpp:44: error: expected primary-expression before '>' token
    I thought, going on like with the other operator I overloaded, *=, would work just fine...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "=>" means what in C++ ?

    Edit: typo of ">=" or does it really exist in C++?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I was watching who was one and I saw only you around from the big guys, so I was hoping for your answer.

    And yes, that was it, a silly typo was made on 11/8/2013 at 4:28 in the morning!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no such operator as =>. It is <=. The order of characters matters.

    Unrelated to your problem

    1) it is usually a good idea for an operator>= to be a const function, since it shouldn't change either operand. You've made the parameter const but not the operator (which is telling the compiler that, in an expression of the form "a >= b" that your operator may change a.

    2) Declaring a function argument const gives no benefit if the argument is passed by value (since a copy is passed, any change to the argument doesn't change what was passed). But that's what you have done with operator *=().

    3) Since operator>=() is a member function, there is no need for it to use p.x() rather than p._x. It has access to p's privates, and doesn't really need to use accessor functions. If you insist on using accessors, your code is generally clearer and easier to understand if it is consistent - use "x() >= p.x()" or "_x >= p._x" rather than mixing the two forms.

    4) Be careful with using member names prefixed by an underscore. Names beginning with underscores are reserved identifiers in some circumstances (in the global namespace) and, if you run into clashes with such things in your implementation (compiler and library), the resultant program bugs tend to be VERY difficult to track down.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Statha was a bit faster.

    I just came across the problem with the const thing.

    I like the point 3 you said and for the point 4 I agree.

    Well, actually this file is been enhanced by me, so the *=() operator was not written by me. Probably he did it, in order to give a plus in readability.

    Thank you both gentlemen.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It really doesn't make sense to compare points on a member-by-member basis. You should instead be comparing their respective absolute distance from the origin (ie: Point(0, 0, 0)); just take the square root of the sum of the squares of each member. Then you can simply define all of your comparison operators in terms of these two scalar values like so:

    Code:
    friend double compare( const Point& lhs, const Point& rhs )
    {
        return lhs.distance( ) - rhs.distance( );
    }
    friend bool operator < ( const Point& lhs, const Point& rhs )
    {
        return compare( lhs, rhs ) < 0;
    }
    friend bool operator <= ( const Point& lhs, const Point& rhs )
    {
        return compare( lhs, rhs ) <= 0;
    }
    // etc...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sebastiani View Post
    It really doesn't make sense to compare points on a member-by-member basis.
    In real terms, it doesn't make much sense to compare points for anything other than equality and inequality.

    You approach makes even less sense, IMHO, than std10093's. Consider comparing the points (1,0) and (0,1). operator>=() and operator<=() would each return true, according to your approach, while operator<() and operator>() would both return false. The only way an operator==() and operator!=() could behave consistently with that is to report (1,0) and (0,1) as equal.

    There is a reason that std::complex types only support operator==() and operator!=(), but no other comparison operators. Personally, I'd do the same with a point class.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by grumpy View Post
    In real terms, it doesn't make much sense to compare points for anything other than equality and inequality.

    You approach makes even less sense, IMHO, than std10093's. Consider comparing the points (1,0) and (0,1). operator>=() and operator<=() would each return true, according to your approach, while operator<() and operator>() would both return false. The only way an operator==() and operator!=() could behave consistently with that is to report (1,0) and (0,1) as equal.

    There is a reason that std::complex types only support operator==() and operator!=(), but no other comparison operators. Personally, I'd do the same with a point class.
    Yeah, I don't know why, but for some reason I was thinking in terms of vectors! Oh well, that's what I get for posting after biking all day in the hot sun...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yeah, well.

    If you were thinking in terms of std::vector's, they don't support operator>=() either.

    If you were thinking in terms of vectors in (say) cartesian space, it is unusual to describe two vectors as being equal if their lengths are the same, but orientation is different.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Um, yeah, I think I'm just going to go to bed now...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well, actually, yesterday when I went to sleep at 7 in the morning I realized my "fault", so they were already gone, before coming here.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading
    By Clayg in forum C++ Programming
    Replies: 14
    Last Post: 01-13-2012, 01:22 PM
  2. Operator Overloading
    By xiaolim in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2009, 03:51 AM
  3. C++ operator overloading
    By mubashariqbal in forum C++ Programming
    Replies: 19
    Last Post: 07-13-2006, 06:19 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
    By ichijoji in forum C++ Programming
    Replies: 10
    Last Post: 07-17-2005, 02:18 AM