Overloading ==

This is a discussion on Overloading == within the C++ Programming forums, part of the General Programming Boards category; This example is from 'You can program in C++' by Francis Glassborow, but doesnt seem to work. Code: #include <iostream> ...

  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
    22,166
    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).
    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
    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
    22,166
    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".
    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

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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,344
    >> 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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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, 02:01 AM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 10: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, 10: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, 02:58 AM

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