Thread: Overloading ==

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14

    Overloading ==

    This example is from 'You can program in C++' by Francis Glassborow, but doesnt seem to work.

    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <string>
    class isbn10{
    public:
      isbn10( );
      explicit isbn10(std::string const &);
      ~isbn10( );
      bool is_valid( )const;
      void send_to(std::ostream & = std::cout)const;
      void get_from(std::istream & = std::cin);
      bool is_equal(isbn10 const &)const;
      inline bool operator==(isbn10 const & lhs, isbn10 const & rhs){
        return lhs.is_equal(rhs);
      }
    private:
      std::string isbn_;
    };
    isbn10.h:14: error: ‘bool isbn10:perator==(const isbn10&, const isbn10&)’ must take exactly one argument

    How to fix it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change "inline bool operator==" to "friend inline bool operator==". As a friend function, operaor== takes two arguments. As a member function, it takes one argument (the current object, this, is the implicit first argument).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14
    It works perfectly now. Thanks.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or even better, don't make it a friend at all and make it a standalone function (since it doesn't need to access any private data in the class), otherwise it adds dependencies.

    Prefer writing nonmember nonfriend functions: http://www.ubookcase.com/book/Addiso...3586/ch44.html

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    don't make it a friend at all and make it a standalone function (since it doesn't need to access any private data in the class)
    Good catch on the implementation not needing friend access, though friend functions are standalone functions, at least if "standalone" means "free".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Another good thing about making it an external function is that you can have things like this:
    Code:
    bool operator==( const string& lhs, const string& rhs )
    {
        return lhs.compare(rhs);
    }
    
    bool operator==( const string& lhs, const char* rhs )
    {
        return lhs.compare(rhs);
    }
    
    bool operator==( const char* lhs, const string& rhs )
    {
        return rhs.compare(lhs);
    }
    Then you can compare different types no matter which side of the == they're on. If it was a member function, the string example above would only be able to compare: string == string, and string == char*, but not char* == string.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There's no need to write a specialized operator==() for this class, because all data members are held by value.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> There's no need to write a specialized operator==() for this class, because all data members are held by value.
    operator== is not generated by default, so you do have to provide it if you want that functionality. Perhaps you are thinking of operator=.

    Also, cpjust, in your example compare is returning 0 when the values are equal, which means your operator== returns true if the strings are not equal.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    in your example compare is returning 0 when the values are equal
    Doh! I knew there was something I was forgetting...

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    >> There's no need to write a specialized operator==() for this class, because all data members are held by value.
    operator== is not generated by default, so you do have to provide it if you want that functionality. Perhaps you are thinking of operator=.
    Yes, I spaced on that one. 72 hours without meaningful sleep will do that to ya...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  4. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM