Results 1 to 2 of 2

Thread: whats wrong here

  1. #1

    whats wrong here

    I'm doing something wrong here is I usually am doing . Perhaps we can find it. I want to overload == to compare two objects.
    The values are const refernce params, and the function should return one of the bool values true or false. They're suppost to be nonmember functions. Now its in a
    class Rock

    the basic function overload would look like this(I think?!?)
    bool operator==(const Rock &x, const Rock &y)
    if (x==y)
    return true;
    return false;

    Now what would I have to do here to make it a nonmember function?
    would making it a friend make it nonmember?
    O, and its tested like this
    cout << "rock1 == rock2? " << (rock1 == rock2 ? "Yes" : "No") << endl;

    Thanks guys, sorry if that looks messy
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  2. #2
    Regular Member
    Join Date
    Aug 1999
    First, unless it is in the definition of a class--it's a non-member. Friend has nothing to do with it. So, you want a stand alone overloaded operator. Example:

    #include "Rock.hpp"  // The Rock class
    #include <iostream>
    bool operator==(const Rock &, const Rock &);
    bool operator==(const Rock & x, const Rock & y) { return (x==y); } 
    int main(int argc, char **argv){
      Rock rock1, rock2;
      // Assign values to rock1 & rock2 here.
      cout << "rock1 == rock2? " 
           << (rock1 == rock2 ? "Yes" : "No") 
           << endl; 
    }  // End main()


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts