PDA

View Full Version : whats wrong here



Orphious
12-10-01, 03:53 PM
I'm doing something wrong here is I usually am doing :D. 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;
else
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

Stu
12-11-01, 03:42 PM
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()